I want to start by saying this is my first project and I am trying hard to find answers myself before posting here. I thought I had found the code to accomplish this. I have no errors but when I run, the field is still editable. So, how can I disable editing of my field when the rule I have set is true?
if sport.count == 1 {
enterSport.text = sport[0] as String //need to convert to a string
enterSport.editing; false //do not allow editing
} else {
//do nothing
}
I have defined the array previously so the if statement is true. Thank you for you assistance.
enterSport.userInteractionEnabled = false
instead of editing
.
editing
is a read-only property to indicate if the field is currently being edited or not.
Swift 5:
enterSport.isUserInteractionEnabled = false
To summarize the answers and comments above:
editing is a readonly property. You can't set it.
If it's a UITextView, it has an editable property which you can set to false.
If it's a UITextField, you need to use the enabled property or the userInteractionEnabled property. Either of those will prevent editing. As
With Swift 3 Apple changed the property userInteractionEnabled
to isUserInteractionEnabled
.
The code looks like this:
textfield.isUserInteractionEnabled = true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With