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

[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. 消息队列MQ对比

    1.ActiveMQ 是Apache下的一个子项目. 类似于ZeroMQ,它能够以代理人和点对点的技术实现队列.同时类似于RabbitMQ,它少量代码就可以高效地实现高级应用场景.RabbitMQ.Z ...

  2. windows 7中的windows键相关的快捷键

    引用 https://support.microsoft.com/zh-cn/help/976857 Windows 键的位置 如果不清楚 Windows 键的位置,请参照下图: 常用的 Window ...

  3. 409 javascript if and while表达式

    数组定义.特点.运算符:算术运算 ++ --(自减 自加) 赋值运算发 =比较:!= == === 逻辑运算 有 && || ! 正则表达式 修饰符 i:用来表示 g:很少演示(在第一 ...

  4. 激活函数——tanh函数(理解)

    0 - 定义 $tanh$是双曲函数中的一个,$tanh()$为双曲正切.在数学中,双曲正切“$tanh$”是由基本双曲函数双曲正弦和双曲余弦推导而来. $$tanhx=\frac{sinhx}{co ...

  5. DCM、PLL、PMCD、MMCM相关

    摘自网上 : http://xilinx.eetop.cn/viewnews-1482 The DCM is a Digital Clock Manager - at its heart it is ...

  6. 看完此文还不懂NB-IoT,你就过来掐死我吧...【转】

    转自:https://www.cnblogs.com/pangguoming/p/9755916.html 看完此文还不懂NB-IoT,你就过来掐死我吧....... 1 1G-2G-3G-4G-5G ...

  7. Django的邮件发送以及云服务器上遇到的问题

    邮件发送 首先我们的邮箱要开通smtp服务,然后就可以在settings中配置了 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBacken ...

  8. JQuery EasyUI 之 messager基本使用

    ♦ 基本语法:$.messager.alert ( title , msg , icon , fn ): 其中,icon有四种设置:"error"."info" ...

  9. Linux的错误码表

    Linux的错误码表(errno table): _ 124 EMEDIUMTYPE_ Wrong medium type_ 123 ENOMEDIUM__ No medium found_ 122 ...

  10. Laravel 生成migration ,boolean字段字段转为tinyInteger

    Schema::create('consults', function (Blueprint $table) { $table->increments('id'); $table->str ...