您的位置:首页 > 产品设计 > UI/UE

IOS开发:UIAlertView使用

2014-12-31 15:53 337 查看
  UIAlertView是什么就不介绍了

1.基本用法

1 UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"Test"    //标题

2                                               message:@"this is a alert view "   //显示内容

3                                              delegate:nil          //委托,可以点击事件进行处理

4                                     cancelButtonTitle:@"取消"

5                                     otherButtonTitles:@"确定"

6                                                     //,@"其他",    //添加其他按钮 

7                                  nil];

8 [view show];效果图:

 


2.多个按钮  

取消上面代码@“其他”的注释后,运行效果如下

 



可以以此类推,添加多个

3.一些系统样式参数

UIAlertViewStyle这个枚举提供了几个样式

1 typedef NS_ENUM(NSInteger, UIAlertViewStyle) {

2     UIAlertViewStyleDefault = 0,            //缺省样式

3     UIAlertViewStyleSecureTextInput,         //密文输入框

4     UIAlertViewStylePlainTextInput,          //明文输入框

5     UIAlertViewStyleLoginAndPasswordInput      //登录用输入框,有明文用户名,和密文密码输入二个输入框

6 };

使用代码如下:

1     UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"请等待"    //标题

2                                                   message:@"this is a alert view "   //显示内容

3                                                  delegate:nil                //委托,可以点击事件进行处理

4                                         cancelButtonTitle:@"取消"

5                                         otherButtonTitles:@"确定",

6                                                     //,@"其他",    //添加其他按钮

7                          nil];

8 [view setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];   //控制样式效果图:

 



这是参数为:UIAlertViewStyleLoginAndPasswordInput  效果图,其他的自行查看

不过这几个类型,我个人觉得太丑了,不能接受,便自定义了个弹出框,用来接受输入

 

 





实现也不难,有需要的朋友可以联系我

4.判断用户点了哪个按钮

UIAlertView的委托UIAlertViewDelegate ,实现该委托来实现点击事件,如下:

.h文件

1 @interface ViewController : UIViewController<UIAlertViewDelegate> {

2

3 }

在.m实现委托的方法

1 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

2 {

3     NSString* msg = [[NSString alloc] initWithFormat:@"您按下的第%d个按钮!",buttonIndex];

4     NSLog(@"%@",msg);

5 }在这个方法中的参数 buttonIndex,表示的是按钮的索引,上图的三按键 “取消”,“确定”,“其他”对应的索引分别为“0”,“1”,“2”.

用Delegate的方式处理点击时候,会带来一个问题比较麻烦,比如在一个页面里,有好几个UIAlertView的时候,处理点击的时候,会增加处理逻辑的复杂度,得做一些判断

这种情况有一个解决办法,就是用Block,添加Block的回调,代替Delegate,target和selector.(下次展开写这个内容)

5.添加子视图

这个用得也是比较多的,贴几个使用实例

添加 UIActivityIndicatorView

 



 

实现代码:

 1     UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"请等待"

 2                                                   message:nil

 3                                                  delegate:nil               

 4                                         cancelButtonTitle:nil

 5                                         otherButtonTitles:nil,

 6                                                           nil];

 7    

 8     UIActivityIndicatorView *activeView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

 9     activeView.center = CGPointMake(view.bounds.size.width/2.0f, view.bounds.size.height-40.0f);

10     [activeView startAnimating];

11     [view addSubview:activeView];

12    

13     [view show];

添加UITableView

 


这个列表的几行代码也说不清楚,就说下思路吧,UIAlertView之所以有这么大的空间显示UITableView,用了比较取巧的一个办法

1 UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"请选择"

2                                             message:@"\n\n\n\n\n\n\n\n\n\n"

3                                                 delegate:nil               

4                                        cancelButtonTitle:nil     

5                                                  otherButtonTitles:nil,

6                                                            nil];         

7  //其中用了10个换行符来撑大UIAlertView的然后再来添加UITableView,可以自行实现,如果有需要,请留言

基本上这是一些比较常用且实用的东西了,然后还有一个比较重要的东西,就是自定义和美化UIAlertView,相信很多人关心这个,自定义和美化的内容放在下一篇来细说,分析几个个人觉得不错的Demo源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: