简体   繁体   English

禁止编辑UITextField但检测点击

[英]Prevent UITextField editing but detect taps

I have a UITextField and I am trying to detect the taps on it and prevent user typing input at the same time. 我有一个UITextField,我试图检测它的水龙头,并防止用户同时键入输入。 The scenario is, I'm gonna use an alert view to make the user select an option and write it inside UITextField rather than user typing it using keyboard. 场景是,我将使用警报视图来使用户选择一个选项并将其写入UITextField中,而不是用户使用键盘键入它。

I tried using in viewDidLoad() : 我尝试在viewDidLoad()

myTextField.delegate = self
myTextField.isEnabled = true
myTextField.isUserInteractionEnabled = false

// for keyboard
myTextField.inputView = UIView()

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didRecognizeTapGesture(_:)))
myTextField.addGestureRecognizer(tapGesture)

However this tap gesture doesn't work: 但是,此轻击手势不起作用:

private dynamic func didRecognizeTapGesture(_ gesture: UITapGestureRecognizer) {
    print("YYYY")
    let point = gesture.location(in: gesture.view)
    guard gesture.state == .ended, taxableField.frame.contains(point) else { return }
    //doSomething()
}

Then I tried making userInteractionEnabled true and this time: 然后我尝试将userInteractionEnabled设置为true,这一次:

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    if textField == myTextField {
        print("XXXX")
        return true
    }
    return true
}

This worked, however now I can long-press and paste into the textfield, so it is active, it's just not showing the keyboard this way. 这行得通,但是现在我可以长按并粘贴到文本字段中,因此它处于活动状态,只是没有以这种方式显示键盘。


So any of the ways I have tried don't seem convenient. 因此,我尝试过的任何方法似乎都不方便。 What do you suggest for achieving such thing? 您对实现这种目标有何建议?

So, to create a UIPickerView and add it as your inputView, you need a delegate and a datasource. 因此,要创建UIPickerView并将其添加为inputView,需要一个委托和一个数据源。 For this example, I'm going to assume that your VC will fill all of these roles. 对于此示例,我将假定您的VC将担任所有这些角色。 I usually split these out into custom classes, but I want to keep this simple: 我通常将它们分成自定义类,但我想保持简单:

// Data source for your picker view
var pickerViewData : [String]?
var myPickerView = UIPickerView()
@IBOutlet myTextField : UITextField!
func viewDidload() {
    myPickerView.delegate = self
    myPickerView.dataSource = self
    myTextField.delegate = self
    myTextField.inputView = pickerView
}

And then, to update your UITextField, whenever your picker view is used, implement the correct delegate method (It won't build unless you implement the method, regardless): 然后,要更新UITextField,无论何时使用选择器视图,都应实现正确的委托方法(除非您实现该方法,否则它将不会生成):

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    myTextField.text = pickerViewData![pickerView.selectedRow(inComponent: 0)]
}

And that's it! 就是这样! You also have to implement some other UIPickerView and UITextField delegate and datasource methods; 您还必须实现其他一些UIPickerView和UITextField委托和数据源方法。 I'm assuming you can figure out how to do this, if you don't know already. 我假设您不知道该怎么做。

You can take a UIButton over the UITextField and align its leading, trailing, top and Bottom with the UITextField . 您可以将UIButton放在UITextField并将其前导,尾随,顶部和底部与UITextField对齐。

Then add an action for the button. 然后为按钮添加一个动作。

Disable the TextField. 禁用TextField。

You can use UIPickerView to implement your function. 您可以使用UIPickerView来实现您的功能。 If the picker must be a AlertView, you can replace the UITextField with a UILabel and put a UIButton upon it then you can implement the custom action in the UIButton callback and display the selection in the UILabel. 如果选择器必须是AlertView,则可以将UITextField替换为UILabel并在其上放置UIButton,然后可以在UIButton回调中实现自定义操作,并在UILabel中显示选择。

create a custom UIViewController which background color is clear and add a UIPickerView to show options. 创建一个自定义的UIViewController,背景颜色清晰,并添加一个UIPickerView来显示选项。 Show the UIViewController (overlay on top of current UIView) while UITextField is focus. 当UITextField成为焦点时,显示UIViewController(在当前UIView上方叠加)。 So, you can do your handles within custom UIViewController. 因此,您可以在自定义UIViewController中进行处理。

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

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