iOS项目开发实战——监听对话框的按钮点击事件

来源:互联网 发布:大数据的意义包括 多选 编辑:程序博客网 时间:2024/06/05 08:52

       有时候App弹出一个提示对话框,需要用户进行点击确定或者取消按钮,此时就需要监听按钮点击事件,这个应该怎么实现呢?

(1)代码实现如下:

#import "ViewController.h"@interface ViewController ()<UIAlertViewDelegate>@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];    [self showAlertDialog];  }-(void)showAlertDialog{  UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:                            @"提示" message:@"欢迎使用App" delegate:self                                           cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];  [alertView show];}- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{  switch (buttonIndex) {    case 0:      NSLog(@"点击了确定按钮");            break;          case 1:      NSLog(@"点击了取消按钮");            break;          default:      break;  }}@end

(2)运行程序,效果如下:


(3)点击不同的按钮,可以在控制台输出不同的消息。代码主要就实现了一个代理:UIAlertViewDelegate中的一个方法。


github主页:https://github.com/chenyufeng1991  。欢迎大家访问!

1 0
原创粉丝点击