zoukankan      html  css  js  c++  java
  • iOS 对UIAlertController内的输入框进行输入监听,实时改变确定、取消按钮颜色

    1、创建UIAlertController

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"新建" message:@"" preferredStyle:UIAlertControllerStyleAlert];

    2、添加一个输入框 添加输入框文字改变监听

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){

            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];

            textField.placeholder = @"请输入全称";

        }];

    3、在确定和取消按钮事件中相应的移除监听

    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

            [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];

        }];

        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

            UITextField * wordNameTextField = alertController.textFields.firstObject;//获取到textField

            [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];

        }];

        okAction.enabled = NO;//此处确定按钮在输入框内没有内容时置灰不可用

        [alertController addAction:cancleAction];

        [alertController addAction:okAction];

        [self presentViewController:alertController animated:YES completion:nil];

    4、实现监听方法

    - (void)alertTextFieldDidChange:(NSNotification *)notification{

        UIAlertController *alertController = (UIAlertController *)self.presentedViewController;

        if (alertController) {

            UITextField *wordNameTextField = alertController.textFields.firstObject;

            UIAlertAction *okAction = alertController.actions.lastObject;

            okAction.enabled = ![@"" isEqualToString:wordNameTextField.text];//输入框有内容时可用

        }

    }

  • 相关阅读:
    ectouch第四讲 之缓存文件的生成
    ectouch第三讲之加载调用机制
    Ubuntu 16.04下安装sublime Text的插件
    有关于Git操作(持续更新)
    MongoDB简单查询语句<平时使用语录,持续更新>
    Ruby小白入门笔记之<Rubymine工具的快捷键>
    Ruby小白入门笔记之 <Gemfile 文件>
    Ubuntu 16.04安装、卸载mysql及怎么使用SQL常用操作语句
    Git中.gitignore忽略文件(maven项目)
    Git入门之常用命令(转载阮一峰老师)
  • 原文地址:https://www.cnblogs.com/chzheng/p/7851852.html
Copyright © 2011-2022 走看看