IOS开发:UIAlertView使用

 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源码

 


<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 16
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值