iOS开发:UIAlertView

之前看过几种UIAlertView的使用,现总结一下,图片来至红黑联盟;

1.基本用法


1 UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"Test"    //标题
                                              message:@"this is a alert view "   //显示内容
                                             delegate:nil          //委托,可以点击事件进行处理
                                    cancelButtonTitle:@"取消"
                                    otherButtonTitles:@"确定"
                                                    //,@"其他",    //添加其他按钮 
7                                  nil];


\

2.多个按钮  

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

 

\

3.一些系统样式参数

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

1 typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
    UIAlertViewStyleDefault = 0,            //缺省样式
    UIAlertViewStyleSecureTextInput,         //密文输入框
    UIAlertViewStylePlainTextInput,          //明文输入框
    UIAlertViewStyleLoginAndPasswordInput      //登录用输入框,有明文用户名,和密文密码输入二个输入框
6 };

使用代码如下:


    UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"请等待"    //标题
                                                  message:@"this is a alert view "   //显示内容
                                                 delegate:nil                //委托,可以点击事件进行处理
                                        cancelButtonTitle:@"取消"
                                        otherButtonTitles:@"确定",
                                                    //,@"其他",    //添加其他按钮
                         nil];
8 [view setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];   //控制样式效果图:

 

\

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

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

.h文件

1 @interface ViewController : UIViewController {
2
3 }

在.m实现委托的方法

1 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
2 {
    NSString* msg = [[NSString alloc] initWithFormat:@"您按下的第%d个按钮!",buttonIndex];
    NSLog(@"%@",msg);
5 }在这个方法中的参数 buttonIndex,表示的是按钮的索引,上图的三按键 “取消”,“确定”,“其他”对应的索引分别为“0”,“1”,“2”.

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

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

5.添加子视图

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

添加 UIActivityIndicatorView

 

\

 

实现代码:


     UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"请等待"
                                                   message:nil
                                                  delegate:nil               
                                         cancelButtonTitle:nil
                                         otherButtonTitles:nil,
                                                           nil];
    
     UIActivityIndicatorView *activeView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
     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:@"请选择"
                                            message:@"\n\n\n\n\n\n\n\n\n\n"
                                                delegate:nil               
                                       cancelButtonTitle:nil     
                                                 otherButtonTitles:nil,
                                                           nil];        


5.使用Block回调
.h文件
  1. -(void) handlerClickedButton:(void (^)(NSInteger btnIndex))aBlock;  .
.m文件
  1. -(void) handlerClickedButton:(void (^)(NSInteger btnIndex))aBlock{  
  2.     self.delegate self 
  3.     objc_setAssociatedObject(selfUIActionSheet_key_clicked, aBlock, OBJC_ASSOCIATION_COPY);  
  4.  
  5.   
  6. -(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{  
  7.     void (^block)(NSInteger btnIndex) objc_getAssociatedObject(selfUIActionSheet_key_clicked);  
  8.       
  9.     if (block) block(buttonIndex);  
  10. }  
按照前面把UIAlertView委托写一下
  1. @interface UIAlertView (Block)  
  2.   
  3. -(void) handlerClickedButton:(void (^)(NSInteger btnIndex))aBlock;  
  4. -(void) handlerCancel:(void (^)(void))aBlock;  
  5. -(void) handlerWillPresent:(void (^)(void))aBlock;  
  6. -(void) handlerDidPresent:(void (^)(void))aBlock;  
  7. -(void) handlerWillDismiss:(void (^)(NSInteger btnIndex))aBlock;  
  8. -(void) handlerDidDismiss:(void (^)(NSInteger btnIndex))aBlock;  
  9. -(void) handlerShouldEnableFirstOtherButton:(BOOL (^)(void))aBlock;  
  10.   
  11. @end  
调用
  1. UIAlertView *alertView [[[UIAlertView alloc] initWithTitle:@"title" message:@"msg" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil nil] autorelease];  
  2. [alertView handlerClickedButton:^(NSInteger btnIndex)  
  3.     NSLogD(@"%d"btnIndex);  
  4. }];  
  5. [alertView show]; 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值