【iOS】按钮点击弹窗

来源:互联网 发布:小米6陶瓷尊享版 知乎 编辑:程序博客网 时间:2024/06/05 11:14

拖入一个Round Rect Button,并将Button的文字修改成“点击弹窗”


将ViewController.h修改为如下代码,实则在ViewController.h中添加了一行-(IBAction)messageBoxShow;,注册messageBoxShow这个函数。类似于C语言使用函数之前需要在头文件声明这个函数头。

////  ViewController.h//  MessageBox////  Created by pc on 17-5-13.//  Copyright (c) 2017年 pc. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController-(IBAction)messageBoxShow;@end
将ViewController.m修改为如下的代码,实则添加-(IBAction)messageBoxShow函数,并完成这个函数的内容。

////  ViewController.m//  MessageBox////  Created by pc on 17-5-13.//  Copyright (c) 2017年 pc. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}-(IBAction)messageBoxShow{    UIAlertView *uiAlertView=[[UIAlertView alloc]initWithTitle:@"弹窗标题栏"message:@"弹窗消息"delegate:nil cancelButtonTitle:@"确定!" otherButtonTitles:nil];    [uiAlertView show];    }@end
回到MainStoryboard.storyboard,单击按钮之后,按着Ctrl出现蓝色箭头,将其拖到View Controller,松开鼠标和Ctrl,指定点击事件。


之后会弹出菜单,选择Sent Events下面的messageBoxShow函数。


点击运行之后,点击按钮则可以弹窗,如下图所示。


0 0
原创粉丝点击