【发布时间】:2018-03-03 12:50:36
【问题描述】:
我正在尝试使用文本字段显示 UIAlertController。当它启动时,键盘会自动显示为文本字段,自动获得焦点。如何在没有键盘的情况下显示带有文本字段的警报(它应该仅在用户单击文本字段时显示)。
这是我的代码
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Collect Input" message:@"input message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//use alert.textFields[0].text
}];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" handler:^(UIAlertAction * action) {
//cancel action
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
// A block for configuring the text field prior to displaying the alert
//[textField resignFirstResponder];
}];
[alert addAction:defaultAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:NO completion:nil];
【问题讨论】:
-
首先想到的是在配置处理程序中 setUserInteractionEnabled = false 并立即执行 dispatch_async() 以重新启用它。如果做不到这一点,则 dispatch_async() 会退出第一响应者
-
文本字段/文本视图在成为第一响应者时显示键盘。最有可能的是,警报控制器是由于被呈现而导致的。您可以像@ekscripto 建议的那样暂时禁用用户交互。
标签: ios objective-c keyboard uialertcontroller