IOS UIAlertView(警告框)方法总结

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

一、初始化方法

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

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

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

效果如下:

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

如果按钮数量超出屏幕显示范围,则会创建类似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;

风格的枚举如下

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;

时间: 2024-08-05 03:04:47

IOS UIAlertView(警告框)方法总结的相关文章

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

一.初始化方法 - (instancetype)initWithTitle:(NSString *)title message:(NSString*)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...; 这个方法通过设置一个标题,内容,

iOS 自定义警告提示(方法+宏)--模仿MJ

1,方法 最好是单独弄到一个类中,定义为类方法. 都知道iOS的debug与release,所以我一开始就定义了一个这样的宏: #ifdef DEBUG #define ZHHLog(...) NSLog(__VA_ARGS__) #else #define ZHHLog(...) #endif #define MAKECHAR(param) @#param 图.h 图.m 使用方法 2,宏 定义宏,没有上面的麻烦,直接弄到一个公共的.h中就OKle. 代码: #ifdef DEBUG #def

警告框和操作表

应用如何与用户交流呢? 警告框(AlertView)和操作表(ActionSheet)就是为此而设计的. 本文案例的原型草图如图3-48所示,其中有两个按钮“Test警告框”和“Test操作表”,点击“Test警告 框”按钮时弹出警告框,它有两个按钮.当点击“Test操作表”按钮时,屏幕下方将滑出操作表. 一.警告框AlertView 警告框是UIAlertView创建的,用于给用户以警告或提示,最多有两个按钮,超过两个就应该使用操作表.由于在iOS中,警告框是“模态”的1,因此不应该随意使用.

iOS:提示框(警告框)控件UIAlertView的详解

提示框(警告框)控件:UIAlertView 功能:当点击按钮或标签等时,弹出一个提示框,显示必要的提示,然后通过添加的按钮完成需要的功能. 类型:typedef NS_ENUM(NSInteger, UIAlertViewStyle) { UIAlertViewStyleDefault = 0,                 //默认类型 UIAlertViewStyleSecureTextInput,          //安全密码的文本框输入类型 UIAlertViewStylePlai

设置警告框样式为带有两个文本输入的警告框,用于收集用户收货地址和联系电话。并选择合适的代理方法,当警告框上的两输入框有一个为空时限制“购买”按钮点击。

收集购物信息  iOS项目 倒计时:588 步骤 /.panel-heading 项目需求 设置警告框样式为带有两个文本输入的警告框,用于收集用户收货地址和联系电话.并选择合适的代理方法,当警告框上的两输入框有一个为空时限制“购买”按钮点击. #import "TableViewController.h" @interface TableViewController ()<UIAlertViewDelegate> @property (nonatomic, strong)

IOS UIAlertController 弹框 (ios 9.0 后代替了UIAlertView弹框 和 UIActionSheet下弹框)

在IOS 9.0 后 苹果官方宣布不再或不推荐使用UIAlertView 和 UIActionSheet 由UIAlertController进行代替两者 用控制器将两者合二为一 很简单 方便 下面就是关于UIAlertView的常用方法 #import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad {

iOS:提示框(警告框)控件UIActionSheet的详解

提示框(警告框)控件2:UIActionSheet 功能:当点击按钮或标签等时,弹出一个提示框,显示必要的提示,然后通过添加的按钮完成需要的功能.它与导航栏类似,它继承自UIView. 风格类型: typedef NS_ENUM(NSInteger, UIActionSheetStyle) { UIActionSheetStyleAutomatic        = -1,       //iOS系统自动默认的风格 UIActionSheetStyleDefault          = UIB

selenium+webdriver+python 中警告框的处理方法

在自动化测试过程中,经常会遇到弹出警告框的情况,如图所示: 在 WebDriver 中处理 JavaScript 所生成的 alert.confirm 以及 prompt 是很简单的.具体做法是使用 switch_to_alert()方法定位到 alert/confirm/prompt.然后使用 text/accept/dismiss/send_keys 按需进行操做.1. 获取警告框的text消息 2. 接受消息框(确定) 3. 取消 4. 输入值 text 返回 alert/confirm/

警告框和操作表(IOS开发)

警告框(AlertView)时模态的,不关闭它就不能做其它事情,所以不是下面几种情况不应该随便使用. 1.应用不能继续执行. 如内存不足,没有网络.一般仅仅须要一个button. 2.询问还有一个解决方式. 不能执行时,询问能否够用3G网络. 3.询问对操作的授权. 涉及到訪问隐私信息的时候,须要用户授权,如位置.相冊等. 操作表(ActionSheet)能够给用户提供多个选择.能够利用它将某个图片发给新浪微博或者Facebook平台. / 实现UIAlertViewDelegate // 这个