Reputation: 12847
I have a UITextField and I am trying to detect the taps on it and prevent user typing input at the same time. 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.
I tried using in 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:
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?
Upvotes: 0
Views: 1532
Reputation: 73
create a custom UIViewController which background color is clear and add a UIPickerView to show options. Show the UIViewController (overlay on top of current UIView) while UITextField is focus. So, you can do your handles within custom UIViewController.
Upvotes: 0
Reputation: 344
You can take a UIButton
over the UITextField
and align its leading, trailing, top and Bottom with the UITextField
.
Then add an action for the button.
Disable the TextField.
Upvotes: 1
Reputation: 1770
So, to create a UIPickerView and add it as your inputView, you need a delegate and a datasource. For this example, I'm going to assume that your VC will fill all of these roles. 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):
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; I'm assuming you can figure out how to do this, if you don't know already.
Upvotes: 1
Reputation: 445
You can use UIPickerView to implement your function. 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.
Upvotes: 0