标签:
一、效果
二、实现
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self alertTest3];
}
/**
* 文本对话框
*/
- (void)alertTest3
{
//1.创建UIAlertController
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"文本对话框"
message:@"登陆和密码对话框示例"
preferredStyle:UIAlertControllerStyleAlert];
//2.添加文本对话框
/**
好处:
1.可以向对话框中添加 任意个 UITextField对象
2.可以使用所有UITextField特性
*/
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"登录";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;//密文
}];
//3.创建UIAlertAction实例
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {//点击“好的”按钮,让程序读取文本框中的值,然后做一些事情
UITextField *login = alertController.textFields.firstObject;
UITextField *password = alertController.textFields.lastObject;
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
//4.UIAlertAction实例添加到alertController
[alertController addAction:okAction];
[alertController addAction:cancelAction];
//5.显示视图控制器
[self presentViewController:alertController animated:YES completion:^{
}];
}
UIAlertController类--alert弹框3(文本对话框)
标签:
原文地址:http://www.cnblogs.com/M-Y-P/p/4972577.html