您的位置:首页 > 移动开发 > IOS开发

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

2015-09-20 15:23 645 查看
       有时候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  。欢迎大家访问!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: