UIAlertController



    // 初始化方法
    + (
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*********/ 

  /*
     ******
添加按钮 ******
     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 ;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值