使用 UIAlertController 防止键盘自动出现
Prevent keyboard from automatically appearing with UIAlertController
我在 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)
这应该可以解决问题:
让你的viewController符合UITextFieldDelegate
将popup.textFields![0].delegate
分配给self
将唯一标签添加到 popup.textFields![0]
(我在下面的示例中使用了 999)
实现这个
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if textField.tag == 999 {
textField.tag = 0
return false
}else{
return true
}
}
您的代码应如下所示:
let popup = UIAlertController(title: "My title",
message: "My message",
preferredStyle: .Alert)
popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in
optionalTextField.placeholder = "This is optional"
}
popup.textFields![0].delegate = self
popup.textFields![0].tag = 999
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)
我认为这是警报中文本字段的默认行为,可以考虑另一种设计,以便文本字段仅在必要时显示...
现在让我们绕过这个!
当您添加 textField 时,使您的 viewController 成为委托并向其添加标签。
例如
popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in
optionalTextField.placeholder = "This is optional"
optionalTextField.delegate = self
optionalTextField.tag = -1
}
然后执行 textFieldShouldBeginEditing()
func textFieldShouldBeginEditing(textField: UITextField!) {
if textField.tag == -1 {
textField.tag = 0
return false
} else {
return true
}
}
我在 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)
这应该可以解决问题:
让你的viewController符合UITextFieldDelegate
将popup.textFields![0].delegate
分配给self
将唯一标签添加到 popup.textFields![0]
(我在下面的示例中使用了 999)
实现这个
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if textField.tag == 999 {
textField.tag = 0
return false
}else{
return true
}
}
您的代码应如下所示:
let popup = UIAlertController(title: "My title",
message: "My message",
preferredStyle: .Alert)
popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in
optionalTextField.placeholder = "This is optional"
}
popup.textFields![0].delegate = self
popup.textFields![0].tag = 999
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)
我认为这是警报中文本字段的默认行为,可以考虑另一种设计,以便文本字段仅在必要时显示...
现在让我们绕过这个!
当您添加 textField 时,使您的 viewController 成为委托并向其添加标签。
例如
popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in
optionalTextField.placeholder = "This is optional"
optionalTextField.delegate = self
optionalTextField.tag = -1
}
然后执行 textFieldShouldBeginEditing()
func textFieldShouldBeginEditing(textField: UITextField!) {
if textField.tag == -1 {
textField.tag = 0
return false
} else {
return true
}
}