zoukankan      html  css  js  c++  java
  • 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     [view show];
     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:@"
    
    
    
    
    
    
    
    
    
    "
    3                                                 delegate:nil                
    4                                        cancelButtonTitle:nil      
    5                                                  otherButtonTitles:nil,
    6                                                            nil];          
    7  //其中用了10个换行符来撑大UIAlertView的

    然后再来添加UITableView,可以自行实现,如果有需要,请留言

    基本上这是一些比较常用且实用的东西了,然后还有一个比较重要的东西,就是自定义和美化UIAlertView,相信很多人关心这个,自定义和美化的内容放在下一篇来细说,分析几个个人觉得不错的Demo源码

    檀庭兵
    QQ:448763423
    Email:tanysang@126.com
  • 相关阅读:
    [LeetCode 1029] Two City Scheduling
    POJ 2342 Anniversary party (树形DP入门)
    Nowcoder 106 C.Professional Manager(统计并查集的个数)
    2018 GDCPC 省赛总结
    CF 977 F. Consecutive Subsequence
    Uva 12325 Zombie's Treasure Chest (贪心,分类讨论)
    Poj 2337 Catenyms(有向图DFS求欧拉通路)
    POJ 1236 Network of Schools (强连通分量缩点求度数)
    POJ 1144 Network (求割点)
    POJ 3310 Caterpillar(图的度的判定)
  • 原文地址:https://www.cnblogs.com/razioooooo/p/3224133.html
Copyright © 2011-2022 走看看