UIAlertController和UIAlertView

来源:互联网 发布:苹果一键刷机软件 编辑:程序博客网 时间:2024/06/08 14:48
注:IOS8之后苹果推出UIAlertController,建议使用
--------------------UIAlertControll------------------
创建方式一:(默认提示框)  
var alertVC = UIAlertController()
alertVC.title = "提示"
alertVC.message = "提示内容"
self.presentViewController(alertVC, animated: true, completion: nil)

创建方式二:(自定提示框+文本对话框)
//初始化 Alert Controller
let alertController = UIAlertController(title: "提示", message: "提示内容", preferredStyle: .Alert)

//创建第一个TextField
alert.addTextFieldWithConfigurationHandler {
(tField:UITextField!) in
tField.placeholder = "用户名" //创建占位符
}

//创建第二个TextField
alert.addTextFieldWithConfigurationHandler { (textField:UITextField!) in
textField.placeholder = "密码"
textField.secureTextEntry = true //安全文字输入
}

//设置 Actions
let yesAction = UIAlertAction(title: "确定", style: .Default){ (action) -> Void in
println("点击了确定按钮")
}
let noAction = UIAlertAction(title: "取消", style: .Default){ (action) -> Void in
println("点击了取消按钮!")
}

noAction.enabled = false //设置取消按钮不可点击

//添加 Actions,添加的先后和显示的先后顺序是有关系的
alertController.addAction(yesAction)
alertController.addAction(noAction)

//展示Alert Controller
self.presentViewController(alertController, animated: true, completion: nil)
---------------------UIAlertView---------------------
创建
let alert = UIAlertView(title: "提示", message: "提示内容", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "确定")
alert.show()

属性
title——标题
message——内容
numberOfButtons——获取按钮的数量
cancelButtonIndex——将某一个按钮设置为取消按钮
firstOtherButtonIndex——返回其他类型按钮 第一个的索引值
visible——警告框是否可见
alertViewStyle——设置警告框风格
typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
UIAlertViewStyleDefault = 0,//默认风格
UIAlertViewStyleSecureTextInput,//密码输入框风格
UIAlertViewStylePlainTextInput,//普通输入框风格
UIAlertViewStyleLoginAndPasswordInput//账号密码框风格
};

方法
显示警告框
- (void)show

代码模拟点击,触发消失效果:定时关闭警告框的时候可用到
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

添加一个按钮,并设置文字,返回的是此按钮的索引值
- (NSInteger)addButtonWithTitle:(NSString *)title;

根据按钮索引,返回按钮标题
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;

这个方法设置文本输入框的索引
根据输入框的索引返回这个输入框
- (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;





0 0
原创粉丝点击