这时候可以添加如下代码:

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// 可以在这里对textfield进行定制,例如改变背景色
textField.backgroundColor = [UIColor orangeColor];
}];

完整的密码输入:

- (void)showSecureTextEntryAlert {
NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];
textField.secureTextEntry = YES;
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"The \"Secure Text Entry\" alert's cancel action occured.");
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
}];

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"The \"Secure Text Entry\" alert's other action occured.");
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
}];

// The text field initially has no text in the text field, so we'll disable it.
otherAction.enabled = NO;
// Hold onto the secure text alert action to toggle the enabled/disabled state when the text changed.
self.secureTextAlertAction = otherAction;

[alertController addAction:cancelAction];
[alertController addAction:otherAction];

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

注意四点:

1.添加通知,监听textfield内容的改变:

// Add the text field for the secure text entry.
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// Listen for changes to the text field's text so that we can toggle the current
// action's enabled property based on whether the user has entered a sufficiently
// secure entry.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];

textField.secureTextEntry = YES;
}];
2.初始化时候,禁用“ok”按钮:

otherAction.enabled = NO;

self.secureTextAlertAction = otherAction;//定义一个全局变量来存储

3.当输入超过5个字符时候,使self.secureTextAlertAction = YES:

- (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification {
UITextField *textField = notification.object;
// Enforce a minimum length of >= 5 characters for secure text alerts.
self.secureTextAlertAction.enabled = textField.text.length >= 5;
}
4.在“OK”action中去掉通知:

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"The \"Secure Text Entry\" alert's other action occured.");

// Stop listening for text changed notifications.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
}];

UIAlertController中TextField的用法的更多相关文章

  1. java中this的用法?

    2008-07-28 08:10cztx5479 | 分类:JAVA相关 | 浏览4533次 java中this的用法? import java.awt.*; import java.awt.even ...

  2. iOS UIAlertController中加入倒计时,输入框,Swift讲解

    一.倒计时 @interface ViewController () { UIAlertController *alertview; NSString * message; NSTimer * wai ...

  3. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  4. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

  5. SQL中distinct的用法

    SQL中distinct的用法   1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...

  6. Oracle 中 decode 函数用法

    Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...

  7. jQuery中Animate进阶用法(一)

    jQuery中animate的用法你了解多少呢?如果仅仅是简单的移动位置,显示隐藏,哦!天哪你在浪费资源!因为animate太强大了,你可以有很多意想不到的用法!让我们一起研究一下吧~~ 首先要了解j ...

  8. [转载]js中return的用法

    一.返回控制与函数结果,语法为:return 表达式; 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果 二.返回控制,无函数结果,语法为:return;  在大多数情况下,为事件处理函 ...

  9. js中this的用法

    经过近几周的模拟面试题,我查询了一些资料,今天就来说说,在js中this的用法吧.方法有四:第一,用作全局变量,第二,用作表该对象,第三,用作构造函数,第四,用作call和applay

随机推荐

  1. 锁定表头和固定列(Fixed table head and columns)

    源码: /// <summary> /// 锁定表头和列 /// <para> sorex.cnblogs.com </para> /// </summary ...

  2. Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays

    题目链接 题意:给你两个数x,yx,yx,y,让你构造一些长为yyy的数列,让这个数列的累乘为xxx,输出方案数. 思路:考虑对xxx进行质因数分解,设某个质因子PiP_iPi​的的幂为kkk,则这个 ...

  3. day14 带参装饰器、迭代器、生成器

    """ 今日内容: 1.带参装饰器及warps 2.迭代器 3.生成器 """ """ # 一.带参装饰器及w ...

  4. Java基础7-异常;jar包

    昨日内容回顾 多态:多种状态,多态的前提条件是继承或者是实现 面向接口编程 降低耦合度 接口是最低标准,接口可以多重继承 接口中的所有方法都是抽象方法 抽象类中可以没有抽象方法 匿名内部类对象 将对类 ...

  5. 【medium】220. Contains Duplicate III

    因为要考虑超时问题,所以虽然简单的for循环也可以做,但是要用map等内部红黑树实现的容器. Given an array of integers, find out whether there ar ...

  6. 将DataTable转换为List<T>对象遇到问题:类型“System.Int64”的对象无法转换为类型“System.Int32”。

    可以利用反射将DataTable转换为List<T>对象:原始链接http://www.jb51.net/article/67386.htm 但是该方法在DataTable里某个字段类型是 ...

  7. vue-地址插件 v-region

    demo地址:https://terryz.gitee.io/vue/#/region/demo

  8. Java_运算符

    目录 一.算术运算符 二.关系运算符 三.位运算符 四.赋值运算符 五.条件运算符 六.instanceof 运算符 七.逻辑运算符 一.算术运算符 加 减 乘 除 取余 自增 自减(+ - * / ...

  9. 最新版 IntelliJ IDEA2018.3.x 破解教程

    https://www.cnblogs.com/Candies/p/10050831.html

  10. sublime的插件

    记录一下常用的插件: 1. htmlpretty 用于HTML.CSS.JS的格式化,以方便阅读代码.插件全名是HTML-CSS-JS Pretty.安装后使用方法是: 打开一个HTML/CSS/JS ...