zoukankan      html  css  js  c++  java
  • UIkit框架之UIalert(iOS 9之后就不用这个了)

    IOS中UIAlertView(警告框)常用方法总结

    一、初始化方法

    - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString*)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;

    这个方法通过设置一个标题,内容,代理和一些按钮的标题创建警告框,代码示例如下:

    1
    2
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"我的警告框" message:@"这是一个警告框" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];

    效果如下:

    130508_XklY_2340880.png

    注意:如果按钮数超过两个,将会创建成如下样子:

    130912_SSoj_2340880.png

    如果按钮数量超出屏幕显示范围,则会创建类似tableView的效果。

    二、属性与方法解析

    标题属性

    @property(nonatomic,copy) NSString *title;

    内容属性

    @property(nonatomic,copy) NSString *message;

    添加一个按钮,返回的是此按钮的索引值

    - (NSInteger)addButtonWithTitle:(NSString *)title;   

    返回根据按钮索引按钮标题 

    - (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;

    获取按钮数量

    @property(nonatomic,readonly) NSInteger numberOfButtons;

    设置将某一个按钮设置为取消按钮

    @property(nonatomic) NSInteger cancelButtonIndex;

    返回其他类型按钮第一个的索引值

    @property(nonatomic,readonly) NSInteger firstOtherButtonIndex;

    警告框是否可见

    @property(nonatomic,readonly,getter=isVisible) BOOL visible;

    显现警告框

    - (void)show;

    代码模拟点击按钮消失触发方法

    - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

    设置警告框风格

    @property(nonatomic,assign) UIAlertViewStyle alertViewStyle;

    风格的枚举如下

    1
    2
    3
    4
    5
    6
    typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
        UIAlertViewStyleDefault = 0,//默认风格
        UIAlertViewStyleSecureTextInput,//密码输入框风格
        UIAlertViewStylePlainTextInput,//普通输入框风格
        UIAlertViewStyleLoginAndPasswordInput//账号密码框风格
    };

    这个方法设置文本输入框的索引

    - (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex;

    三、UIAlertViewDelegate中的方法

    点击按钮时触发的方法

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

    将要展现警告框时触发的方法

    - (void)willPresentAlertView:(UIAlertView *)alertView;

    已经展现警告框时触发的方法

    - (void)didPresentAlertView:(UIAlertView *)alertView;

    警告框将要消失时触发的方法

    - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;

    警告框已经消失时触发的方法

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex; 

    设置是否允许第一个按钮不是取消按钮

    - (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView;

    辅助功能:UIAlertViewStyle的类型:

        UIAlertViewStyleDefault = 0:一个标准的警告框

    UIAlertViewStyleSecureTextInput, :一个能够让用户输入文本的警告框,并且文本框是掩盖的

    UIAlertViewStylePlainTextInput, :一个能够让用户 输入文本的警告框

    UIAlertViewStyleLoginAndPasswordInput :一个能够让用户输入账号和密码的警告框

  • 相关阅读:
    [LeetCode]603. 连续空余座位(Mysql、自连接)
    [LeetCode]671. 二叉树中第二小的节点(递归)
    [LeetCode] 203. 移除链表元素(链表基本操作-删除)、876. 链表的中间结点(链表基本操作-找中间结点)
    [LeetCode]26. 删除排序数组中的重复项(数组,双指针)
    C# 把引用的dll嵌入到exe文件中
    iptables规则表
    [转载]EF Code First 学习笔记:约定配置
    使用itunes同步ios时丢失照片恢复
    USB硬件远程共享解决iphone已停用
    C# 非独占延时函数 非Sleep
  • 原文地址:https://www.cnblogs.com/lelun/p/5681967.html
Copyright © 2011-2022 走看看