美文网首页
iOS UIAlertController-针对UIAlert

iOS UIAlertController-针对UIAlert

作者: 沙漠骑士 | 来源:发表于2016-07-11 14:00 被阅读98次

    不多说直接代码,原理和UIAlertView一样,它的本质是一个控制器所以推出的方式和控制器一样,而不是原来UIAlertView的show方式,值得注意的是必须要是iOS8.0之后,否则适配会有错误

    自己添加一个点击事件
    //这段代码我加入了两个textfield,并且对上面的textfield进行了监控,需要注意的是无论在NO,或是OK中,必须要把监听移除
    - (void)showClick {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Login" message:@"please input" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
            //监听
            /**
             *  UITextFieldTextDidBeginEditingNotification;
             *  UITextFieldTextDidEndEditingNotification;
             *  UITextFieldTextDidChangeNotification;
             */
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
            textField.placeholder = @"username";
        }];
        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"password";
            textField.secureTextEntry = YES;
        }];
    /**
         *  UIAlertActionStyleDefault
         *  UIAlertActionStyleCancel,
         *  UIAlertActionStyleDestructive 体现出红字,主要用在删除等操作
         */
        UIAlertAction *cancel=[UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    //点击NO的操作
            //移除监听
            [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
        }];
        UIAlertAction *defult = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //点击YES的操作
            UITextField *login = alertController.textFields.firstObject;
            UITextField *password = alertController.textFields.lastObject;
            NSLog(@"login:%@",login.text);
            NSLog(@"password:%@",password.text);
            //移除监听
            [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
            [self presentViewController:[[TestViewController alloc]init] animated:YES completion:nil];
            
        }];
      
        [alertController addAction:cancel];
        [alertController addAction:defult];
        defult.enabled = NO;
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    监听方法
    - (void)alertTextFieldDidChange:(NSNotification *)notification{
        UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
        if (alertController) {
            UITextField *login = alertController.textFields.firstObject;
            UIAlertAction *okAction = alertController.actions.lastObject;
            okAction.enabled = login.text.length > 10;
    //        okAction.enabled = [YLUtlis checkTelNumber:login.text];//此处自己可以使用正则
        }
    }
    
    显示的效果
    Paste_Image.png

    我的主页
    沙漠骑士

    相关文章

      网友评论

          本文标题: iOS UIAlertController-针对UIAlert

          本文链接:https://www.haomeiwen.com/subject/nlqjjttx.html