gpt4 book ai didi

ios - 阻止 UITextField 编辑但检测点击

转载 作者:行者123 更新时间:2023-11-28 12:28:54 25 4
gpt4 key购买 nike

我有一个 UITextField,我正在尝试检测其上的点击并防止用户同时键入输入。场景是,我将使用警报 View 让用户选择一个选项并将其写入 UITextField,而不是用户使用键盘键入它。

我尝试在 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)

但是这个点击手势不起作用:

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()
}

然后我尝试让 userInteractionEnabled 为真,这次:

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

这行得通,但是现在我可以长按并粘贴到文本字段中,所以它是事件的,只是不以这种方式显示键盘。


所以我尝试过的任何一种方式似乎都不方便。你对实现这样的目标有什么建议?

最佳答案

因此,要创建一个 UIPickerView 并将其添加为您的 inputView,您需要一个委托(delegate)和一个数据源。对于这个例子,我假设你的 VC 将担任所有这些角色。我通常将它们分成自定义类,但我想保持简单:

// 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
}

然后,要更新您的 UITextField,无论何时使用您的选择器 View ,请实现正确的委托(delegate)方法(无论如何,除非您实现该方法,否则它不会构建):

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

就是这样!您还必须实现其他一些 UIPickerView 和 UITextField 委托(delegate)和数据源方法;我假设您可以弄清楚如何执行此操作,如果您还不知道的话。

关于ios - 阻止 UITextField 编辑但检测点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42638488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com