ios - 我使用此代码弹出一个带有“取消”和“提交”选项的文本字段,但键盘也显示。我不想显示键盘

标签 ios swift uitextfield

我使用此代码弹出一个带有“取消”和“提交”选项的 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)将显示如果我们添加 UITextFieldUIAlertController

因此,根据您的要求,您需要执行以下操作:

首先设置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/

相关文章:

ios - 当我尝试快速绘制渐变时出现问题

ios - 模拟双击 UITabBarController 到 Root View Controller

ios - Swift UITextFieldShouldReturn 返回键点击

ios - 有什么办法,从通知中心获取推送通知? (来自服务器的推送通知)

ios - 关于苹果证书和分发

iOS 如何调整图像和蒙版(图像或蒙版任何人都不能同时放大调整)

Swift:UIGraphicsImageRendererFormat 而不是 UIGraphicsBeginImageContext 返回空白图像

ios - SceneKit 使用枢轴计算节点的顶点世界坐标

ios - Swift iOS - TextField 初始宽度错误的底部边框

ios - 如何防止对象在当前作为委托(delegate)时被取消初始化