Disable text field editing

I want to start with the fact that this is my first project, and I try to find the answers myself before publishing here. I thought I found code for this. I have no errors, but when I run, the field is still editable. So, how can I turn off editing my field when the rule I set is correct?

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 predefined an array, so the if statement is true. Thank you for your help.

+6
source share
3 answers

enterSport.userInteractionEnabled = false instead of editing .

editing is a read-only property indicating whether the field is currently being edited.

+9
source

To summarize the answers and comments above:

editing is a readonly property. You cannot install it.

If it is a UITextView, it has an editable property that you can set to false.

If it is a UITextField, you need to use the enabled property or the userInteractionEnabled property. Any of them will prevent editing. Because the

+3
source

With Swift 3, Apple changed the userInteractionEnabled property to isUserInteractionEnabled .

The code is as follows:

textfield.isUserInteractionEnabled = true

+1
source

Source: https://habr.com/ru/post/985917/


All Articles