有时候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 。欢迎大家访问!