iOS UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法

来源:互联网 发布:天气软件好评排行榜 编辑:程序博客网 时间:2024/06/05 00:18

首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定delegate为self,才能使弹出的Alert窗口响应点击事件。

具体代码如下:

ViewController.h中的代码如下:

[cpp] view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<UIAlertViewDelegate>  
  4.   
  5. @end  


ViewController.m中的详细代码:

[java] view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view from its nib  
  5.       
  6.     //初始化AlertView  
  7.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertViewTest"  
  8.                                                    message:@"message"  
  9.                                                   delegate:self  
  10.                                          cancelButtonTitle:@"Cancel"  
  11.                                          otherButtonTitles:@"OtherBtn",nil];  
  12.     //设置标题与信息,通常在使用frame初始化AlertView时使用  
  13.     alert.title = @"AlertViewTitle";  
  14.     alert.message = @"AlertViewMessage";  
  15.       
  16.     //这个属性继承自UIView,当一个视图中有多个AlertView时,可以用这个属性来区分  
  17.     alert.tag = 0;  
  18.     //只读属性,看AlertView是否可见  
  19.     NSLog(@"%d",alert.visible);  
  20.     //通过给定标题添加按钮  
  21.     [alert addButtonWithTitle:@"addButton"];  
  22.     //按钮总数  
  23.     NSLog(@"number Of Buttons :%d",alert.numberOfButtons);  
  24.     //获取指定索引的按钮标题  
  25.     NSLog(@"buttonTitleAtIndex1:%@",[alert buttonTitleAtIndex:1]);  
  26.     NSLog(@"buttonTitleAtIndex2:%@",[alert buttonTitleAtIndex:2]);  
  27.     //获取取消按钮的索引  
  28.     NSLog(@"cancelButtonIndex:%d",alert.cancelButtonIndex);  
  29.     //获取第一个其他按钮的索引  
  30.     NSLog(@"firstOtherButtonIndex:%d",alert.firstOtherButtonIndex);  
  31.     //显示AlertView  
  32.     [alert show];  
  33.     [alert release];  
  34. }  
  35.   
  36. #pragma marks -- UIAlertViewDelegate --  
  37. //根据被点击按钮的索引处理点击事件  
  38. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  39. {  
  40.     NSLog(@"clickButtonAtIndex:%d",buttonIndex);  
  41. }  
  42.   
  43. //AlertView已经消失时执行的事件  
  44. -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex  
  45. {  
  46.     NSLog(@"didDismissWithButtonIndex");  
  47. }  
  48.   
  49. //ALertView即将消失时的事件  
  50. -(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex  
  51. {  
  52.     NSLog(@"willDismissWithButtonIndex");  
  53. }  
  54.   
  55. //AlertView的取消按钮的事件  
  56. -(void)alertViewCancel:(UIAlertView *)alertView  
  57. {  
  58.     NSLog(@"alertViewCancel");  
  59. }  
  60.   
  61. //AlertView已经显示时的事件  
  62. -(void)didPresentAlertView:(UIAlertView *)alertView  
  63. {  
  64.     NSLog(@"didPresentAlertView");  
  65. }  
  66.   
  67. //AlertView即将显示时  
  68. -(void)willPresentAlertView:(UIAlertView *)alertView  
  69. {  
  70.     NSLog(@"willPresentAlertView");  
  71. }  
  72.   
  73. - (void)viewDidUnload  
  74. {  
  75.     [super viewDidUnload];  
  76.     // Release any retained subviews of the main view.  
  77.     // e.g. self.myOutlet = nil;  
  78. }  
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 海淘转运地址国家填错了怎么办 集装箱实重与申报重量不一样怎么办 微博复制的淘口令找不到了怎么办 买了移动手机不能用联通卡怎么办 移动手机用联通卡网速慢怎么办 移动手机插联通卡没反应怎么办 移动手机办了联通大王卡怎么办 qq被冻结但是有至尊宝怎么办 qq被冻结了有至尊宝怎么办 移动电话卡注销了里面的钱怎么办 罗麦的oa上经理喜报没截图怎么办 工行融e联登录密码忘了怎么办 融e借有额度秒拒怎么办 工行银行柜台办理融e借怎么办 地球末日生存破解版金币没了怎么办 手机被别人骗走了里面的微信怎么办 在微信里面被做微商的骗了钱怎么办 在qq上骗了人50怎么办 被3m多酶清洗液滴到皮肤怎么办 做3m赔了9万怎么办 在携程网上订的酒店发票怎么办 滴滴滴取消订单电话打不通怎么办 百度云容量2068g满了怎么办 淘宝电脑端描述图片间隔大怎么办 手机上下载游戏自动扣费怎么办 苹果手机下载游戏自动扣费怎么办 用手机账号登陆游戏换手机怎么办 在qq上被骗充q币怎么办 微信借钱后删了好友怎么办 财付通绑定了其它人的身份证怎么办 微信支付密码忘了没绑银行卡怎么办 微信没有绑银行卡忘记密码怎么办 威信解绑银行卡支付密码望了怎么办 买了个qq号绑定了财付通怎么办 淘宝买的东西下架了怎么办 微信红包充话费充错了怎么办 手机qq不能发红包或转账怎么办 qq给人发红包被骗了怎么办 qq发红包对方看不不到怎么办 手机浏览器支付页面弹不出来怎么办 新手机登录微信需要验证怎么办