1、在viewController的.m文件,遵守<UIAlertViewDelegate>代理协议
@interface moboViewController () <UIAlertViewDelegate>
2、在 - (void)viewDidLoad 方法中实现UIAlertView
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 5 // 1、初始化 alertView 6 UIAlertView *altView = [[UIAlertView alloc]initWithTitle:[self altTitle] message:[self altMSG] delegate:self cancelButtonTitle:[self cancelBtnTitle] otherButtonTitles:[self otherBtnTitle], nil]; 7 // 2、显示 alertView 8 [altView show]; 9 10 }
3、继续实现其它方法,返回各种标题以及监听按钮点击事件的代理方法
1 //返回 标题 2 - (NSString *)altTitle{ 3 return @"下线通知"; 4 } 5 6 //返回 消息体 7 - (NSString *)altMSG{ 8 return @"你的帐号在异地登录,密码可能泄露,建议前往http://mobodemy.com进行修改。"; 9 }10 11 //返回 退出按钮 标题12 - (NSString *) cancelBtnTitle {13 return @"退出";14 }15 16 //返回 重新登录 按钮标题17 - (NSString *) otherBtnTitle {18 return @"重新登录";19 }20 21 //监听点击事件 代理方法22 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex23 {24 NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex];25 if ([btnTitle isEqualToString:[self cancelBtnTitle]]) {26 NSLog(@"你点击了退出");27 }28 else if ([btnTitle isEqualToString:[self otherBtnTitle]] ) {29 NSLog(@"你点击了重新登录按钮");30 }31 }
新闻热点
疑难解答