繁体   English   中英

我正在使用此代码弹出带有``取消并提交''选项的文本字段,但键盘也显示了。我不想显示键盘

[英]I am using this code for pop up a textfield with Cancel & Submit options, but keyboard is also showing.I don't want to show the keyboard

我正在使用此代码弹出带有“取消”和“提交”选项的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)

    }
}

作为默认行为,如果我们在UIAlertController上添加UITextField ,则将显示Keyboard or UIPickerView (如果inputView设置为UIPickerView)。

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

首先将UITextField的inputview属性设置为UIPickerView并隐藏键盘/ UIPickerView。

textField.inputView = self.pickerView
textField.resignFirstResponder()

然后,声明变量以检查用户是否单击了文本字段。 在这里,我使用isTextFieldClicked作为布尔值。

var isTextFieldClicked = false

更新myTargetFunction以设置值并显示UIPickerView

self.isTextFieldClicked = true
sender.becomeFirstResponder()

UITextField委托方法中,如果isTextFieldClicked值为true,则检查并返回true。

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
            if isTextFieldClicked == false {
                textField.resignFirstResponder()
                return false
            }
            return true
        }

这将解决您的问题,请确保...

干杯!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM