我使用此代码弹出一个带有“取消”和“提交”选项的 TextField
,但键盘也显示了。我不想通过弹出窗口显示键盘。
@IBAction func can(_ sender: UIButton) {
let alertController : UIAlertController = UIAlertController(title: "", message: "Confirm Order ?", preferredStyle: UIAlertController.Style.alert)
alertController.addTextField {
(textField : UITextField!) -> Void in
textField.placeholder = "Expected Time"
textField.addTarget(self, action: #selector(self.myTargetFunction(sender:)), for: .touchDown)
let okAction = UIAlertAction(title: "CANCEL", style: UIAlertAction.Style.destructive) {
(UIAlertAction) in
}
let CANAction = UIAlertAction(title: "SUBMIT", style: UIAlertAction.Style.default) {
(UIAlertAction) in
ValidateClass.showToast(self.view, title: "Alert", Msg: "Please select the expected time", alertType: 1)
}
alertController.addAction(okAction)
alertController.addAction(CANAction)
self.present(alertController, animated: true, completion: nil)
}
}
最佳答案
作为默认行为,Keyboard or UIPickerView
(如果 inputView 设置为 UIPickerView)将显示如果我们添加 UITextField
在 UIAlertController
因此,根据您的要求,您需要执行以下操作:
首先设置UITextField的inputview
属性(property)UIPickerView
并隐藏键盘/UIPickerView。
textField.inputView = self.pickerView
textField.resignFirstResponder()
然后,声明变量来检查用户是否单击了文本字段。这里我用了isTextFieldClicked
作为 bool 值。
var isTextFieldClicked = false
更新myTargetFunction
设置值并显示UIPickerView
self.isTextFieldClicked = true
sender.becomeFirstResponder()
在 UITextField
委托(delegate)方法,检查并返回 true if isTextFieldClicked
值是真实的。
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if isTextFieldClicked == false {
textField.resignFirstResponder()
return false
}
return true
}
这肯定会解决你的问题...
干杯!
关于ios - 我使用此代码弹出一个带有“取消”和“提交”选项的文本字段,但键盘也显示。我不想显示键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57051799/