【发布时间】:2016-04-21 16:29:40
【问题描述】:
我在 Swift 中有一个 UIAlertController(警报风格),一切正常。但是,我添加到其中的UITextField 是一个可选字段,用户不需要在其中输入文本。问题是当我显示这个UIAlertController 时,键盘会与默认选择的文本字段同时出现。我不希望键盘出现,除非用户点击UITextField。如何做到这一点?
let popup = UIAlertController(title: "My title",
message: "My message",
preferredStyle: .Alert)
popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in
optionalTextField.placeholder = "This is optional"
}
let submitAction = UIAlertAction(title: "Submit", style: .Cancel) { (action) -> Void in
let optionalTextField = popup.textFields![0]
let text = optionalTextField.text
print(text)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
popup.addAction(cancelAction)
popup.addAction(submitAction)
self.presentViewController(popup, animated: true, completion: nil)
【问题讨论】:
-
您必须在其他地方进行 becomeFirstResponder 调用。或者您可以在显示警报之后/之前调用 self.view endEditing:YES。
标签: ios swift uitextfield uikeyboard uialertcontroller