UIAlertController 在集成UIAlterView 和 UIActionSheet 功能后, 具有极大的灵活性,意味着您不必拘泥于内置样式。以前我们只能在默认视图、文本框视图、密码框视图、登录和密码输入框视图中选择,现在我们可以向对话框中添加任意数目的UITextField对象,并且可以使用所有的UITextField特性。当您向对话框控制器中添加文本框时,您需要指定一个用来配置文本框的代码块,并配置相应 UIAlterAction按钮(事件) 。
举个例子吧,建立原来的登录和密码样式对话框,我们可以向其中添加两个文本框,然后用合适的占位符来配置它们,最后将密码输入框设置使用安全文本输入
- (void)setPopUpView:(UIButton*)sender{
//创建一个弹框
UIAlertController *addAlertVC = [UIAlertController alertControllerWithTitle:@"这是一个添加数据弹框" message:@"确定要添加数据吗?" preferredStyle:UIAlertControllerStyleAlert];
//创建两个textFiled输入框
[addAlertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请输入用户名";
}];
[addAlertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请输入密码";
textField.secureTextEntry = YES;
}];
//创建取消事件(按钮)
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
//添加 取消事件 到 弹窗界面
[addAlertVC addAction:cancelAction];
//创建 确认按钮(事件) 并添加到弹窗界面
__block UITextField *textTF = [[[UITextField alloc] init] autorelease];
UIAlertAction *confirmAction =[UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
// textTF = [addAlertVC.view viewWithTag:100];
// _label.text = textTF.text;//接收输入框内容
_label.text = addAlertVC.textFields.firstObject.text;
}];
//添加确定按钮(事件)到弹窗
[addAlertVC addAction:confirmAction];
//模态显示一个弹框 注意: UIAlterController只能通过模态形式进行弹出,不能使用push
[self.navigationController presentViewController:addAlertVC animated:NO completion:nil];
}