• IOS开发:UIAlertView使用


    链接地址:http://www.2cto.com/kf/201307/231841.html  

    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:@" "
    3                                                 delegate:nil               
    4                                        cancelButtonTitle:nil     
    5                                                  otherButtonTitles:nil,
    6                                                            nil];         
    7  //其中用了10个换行符来撑大UIAlertView的然后再来添加UITableView,可以自行实现,如果有需要,请留言

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

    如果一件事情你觉得难的完不成,你可以把它分为若干步,并不断寻找合适的方法。最后你发现你会是个超人。不要给自己找麻烦,但遇到麻烦绝不怕,更不要退缩。 电工查找电路不通点的最快方法是:分段诊断排除,快速定位。你有什么启示吗? 求知若饥,虚心若愚。 当你对一个事情掌控不足的时候,你需要做的就是“梳理”,并制定相应的规章制度,并使资源各司其职。
  • 相关阅读:
    zookeeper 简介
    缓存雪崩 缓存穿透
    SpringCloud实战2-Ribbon客户端负载均衡
    SpringCloud实战1-Eureka
    JVM笔记9-Class类文件结构
    JVM笔记8-虚拟机性能监控与故障处理工具
    JVM笔记7-内存分配与回收策略
    SpringAOP-JDK 动态代理和 CGLIB 代理
    MySQL多数据源笔记5-ShardingJDBC实战
    MySQL多数据源笔记4-Mycat中间件实战
  • 原文地址:https://www.cnblogs.com/wvqusrtg/p/5165932.html
Copyright © 2020-2023  润新知