//
初始化方法
+ ( instancetype )alertControllerWithTitle:( nullable NSString *)title message:( nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
//UIAlertControllerStyle
typedef NS_ENUM (NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0 ,
+ ( instancetype )alertControllerWithTitle:( nullable NSString *)title message:( nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
//UIAlertControllerStyle
typedef NS_ENUM (NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0 ,
UIAlertControllerStyleAlert
}
/**************
自动消失的
UIAlertController*********/
//
创建
UIAlertController
UIAlertController *alertController = [ UIAlertController alertControllerWithTitle : @" 标题 " message : @"UIAlertController" preferredStyle : UIAlertControllerStyleAlert ];
[ self presentViewController :alertController animated : YES completion : nil ];
// 线程 2 秒后执行
dispatch_after ( dispatch_time ( DISPATCH_TIME_NOW , ( int64_t )( 2.f * NSEC_PER_SEC )), dispatch_get_main_queue (), ^{
[alertController dismissViewControllerAnimated : YES completion :^{
}];
});
UIAlertController *alertController = [ UIAlertController alertControllerWithTitle : @" 标题 " message : @"UIAlertController" preferredStyle : UIAlertControllerStyleAlert ];
[ self presentViewController :alertController animated : YES completion : nil ];
// 线程 2 秒后执行
dispatch_after ( dispatch_time ( DISPATCH_TIME_NOW , ( int64_t )( 2.f * NSEC_PER_SEC )), dispatch_get_main_queue (), ^{
[alertController dismissViewControllerAnimated : YES completion :^{
}];
});
/**************自动消失的UIAlertController*********/
/*
****** 添加按钮 ******
Title : 标题名称
style : 样式 [Cancle( 取消 ) Default( 默认的 ) destructive( 重置 )]
handler: 处理程序 ( 点击按钮执行的代码 )
*/
/************** 带按钮的的 UIAlertController*********/
// 创建 UIAlertController
UIAlertController *alertController = [ UIAlertController alertControllerWithTitle : @" 标题 " message : @"UIAlertController" preferredStyle : UIAlertControllerStyleAlert ];
// 创建 " 取消 " 样式按钮
UIAlertAction *cancelAction = [ UIAlertAction actionWithTitle : @" 取消 " style : UIAlertActionStyleCancel handler :^( UIAlertAction *action) {
// 添加 点击事件
self . view . backgroundColor = [ UIColor yellowColor ];
}];
// 创建 " 默认 " 样式按钮
UIAlertAction *defaultAction = [ UIAlertAction actionWithTitle : @" 默认 default" style : UIAlertActionStyleDefault handler :^( UIAlertAction * _Nonnull action) {
}];
// 创建 “ 警示 ” 样式按钮
UIAlertAction *destructiveAction = [ UIAlertAction actionWithTitle : @" 重置 deatructive" style : UIAlertActionStyleDestructive handler :^( UIAlertAction * _Nonnull action) {
}];
// 将按钮添加到 alertController 上
[alertController addAction :cancelAction];
[alertController addAction :defaultAction];
[alertController addAction :destructiveAction];
[ self presentViewController :alertController animated : YES completion : nil ];
/************** 带按钮的的 UIAlertController*********/
****** 添加按钮 ******
Title : 标题名称
style : 样式 [Cancle( 取消 ) Default( 默认的 ) destructive( 重置 )]
handler: 处理程序 ( 点击按钮执行的代码 )
*/
/************** 带按钮的的 UIAlertController*********/
// 创建 UIAlertController
UIAlertController *alertController = [ UIAlertController alertControllerWithTitle : @" 标题 " message : @"UIAlertController" preferredStyle : UIAlertControllerStyleAlert ];
// 创建 " 取消 " 样式按钮
UIAlertAction *cancelAction = [ UIAlertAction actionWithTitle : @" 取消 " style : UIAlertActionStyleCancel handler :^( UIAlertAction *action) {
// 添加 点击事件
self . view . backgroundColor = [ UIColor yellowColor ];
}];
// 创建 " 默认 " 样式按钮
UIAlertAction *defaultAction = [ UIAlertAction actionWithTitle : @" 默认 default" style : UIAlertActionStyleDefault handler :^( UIAlertAction * _Nonnull action) {
}];
// 创建 “ 警示 ” 样式按钮
UIAlertAction *destructiveAction = [ UIAlertAction actionWithTitle : @" 重置 deatructive" style : UIAlertActionStyleDestructive handler :^( UIAlertAction * _Nonnull action) {
}];
// 将按钮添加到 alertController 上
[alertController addAction :cancelAction];
[alertController addAction :defaultAction];
[alertController addAction :destructiveAction];
[ self presentViewController :alertController animated : YES completion : nil ];
/************** 带按钮的的 UIAlertController*********/
/**************
带输入框的
UIAlertController*********/
//
创建
UIAlertController
UIAlertController *alertController = [ UIAlertController alertControllerWithTitle : @" 文本对话框 " message : @" 登录和密码对话框示例 " preferredStyle : UIAlertControllerStyleAlert ];
// 添加输入框
[alertController addTextFieldWithConfigurationHandler :^( UITextField *textField){
textField. placeholder = @" 登录 " ;
// 添加通知,监听输入框的变化
[[ NSNotificationCenter defaultCenter ] addObserver : self selector : @selector (alertTextFieldDidChange:) name : UITextFieldTextDidChangeNotification object :textField];
}];
[alertController addTextFieldWithConfigurationHandler :^( UITextField *textField) {
textField. placeholder = @" 密码 " ;
textField. secureTextEntry = YES ;
}];
// 添加按钮
UIAlertAction *okAction = [ UIAlertAction actionWithTitle : @" 好的 " style : UIAlertActionStyleDefault handler :^( UIAlertAction *action) {
// 获取到输入框的内容
UITextField *login = alertController. textFields . firstObject ;
UITextField *password = alertController. textFields . lastObject ;
// 移除通知
[[ NSNotificationCenter defaultCenter ] removeObserver : self name : UITextFieldTextDidChangeNotification object : nil ];
}];
// 冻结按钮
okAction. enabled = NO ;
[alertController addAction :okAction];
[ self presentViewController :alertController animated : YES completion :^{
}];
/************** 带输入框的 UIAlertController*********/
}
// 通知触发的方法
- ( void )alertTextFieldDidChange:( NSNotification *)notification{
UIAlertController *alertController = ( UIAlertController *) self . presentedViewController ;
if (alertController) {
UITextField *login = alertController. textFields . firstObject ;
UIAlertAction *okAction = alertController. actions . lastObject ;
// 当输入的字数大于 2 时解冻
okAction. enabled = login. text . length > 2 ;
}
}
UIAlertController *alertController = [ UIAlertController alertControllerWithTitle : @" 文本对话框 " message : @" 登录和密码对话框示例 " preferredStyle : UIAlertControllerStyleAlert ];
// 添加输入框
[alertController addTextFieldWithConfigurationHandler :^( UITextField *textField){
textField. placeholder = @" 登录 " ;
// 添加通知,监听输入框的变化
[[ NSNotificationCenter defaultCenter ] addObserver : self selector : @selector (alertTextFieldDidChange:) name : UITextFieldTextDidChangeNotification object :textField];
}];
[alertController addTextFieldWithConfigurationHandler :^( UITextField *textField) {
textField. placeholder = @" 密码 " ;
textField. secureTextEntry = YES ;
}];
// 添加按钮
UIAlertAction *okAction = [ UIAlertAction actionWithTitle : @" 好的 " style : UIAlertActionStyleDefault handler :^( UIAlertAction *action) {
// 获取到输入框的内容
UITextField *login = alertController. textFields . firstObject ;
UITextField *password = alertController. textFields . lastObject ;
// 移除通知
[[ NSNotificationCenter defaultCenter ] removeObserver : self name : UITextFieldTextDidChangeNotification object : nil ];
}];
// 冻结按钮
okAction. enabled = NO ;
[alertController addAction :okAction];
[ self presentViewController :alertController animated : YES completion :^{
}];
/************** 带输入框的 UIAlertController*********/
}
// 通知触发的方法
- ( void )alertTextFieldDidChange:( NSNotification *)notification{
UIAlertController *alertController = ( UIAlertController *) self . presentedViewController ;
if (alertController) {
UITextField *login = alertController. textFields . firstObject ;
UIAlertAction *okAction = alertController. actions . lastObject ;
// 当输入的字数大于 2 时解冻
okAction. enabled = login. text . length > 2 ;
}
}