Easy way to disable a UITextField?

Viewed 59841

Is there an easy way to disable a UITextField in code?

My app has 12 UITextField that are all turned on by default, but when a change is detected in my Segment Control I want to disable some of the UITextField depending on what Segment the user picks.

Just need to know how to disable it or make it non-editable?

Thanks

5 Answers

In Objective-C:

textField.enabled = NO;

In Swift:

textField.isEnabled = false

Reference: UIControl.isEnabled

You should conform to UITextFieldDelegate and return false in this method. This way the text field is still selectable, but it is not available for editing and cutting

Code is in Swift 4

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  return false
}
Related