Disable TextFields Then Enable Them

is there a way to type into a textField while having the other textFields disabled but then after you type into that textField having those other textFields you disabled enabled again?


I know how to disable them with .isUserInteractionEnabled but I don't know how to enable them again with func textFieldShouldReturn.

Answered by Claude31 in 650795022

With my textFields there is an option to clear and I don't want them editing the other textField when they don't fill in the one they cleared.

That's new information. It comes piece by piece !

Implement similar code in
Code Block
optional func textFieldShouldClear(_ textField: UITextField) -> Bool

Code Block
if widthPer == textField {
heightPer.isUserInteractionEnabled = false
widthPer.isUserInteractionEnabled = true
} else {
heightPer.isUserInteractionEnabled = true
widthPer.isUserInteractionEnabled = false
}


If that works, don't forget to close the thread on this answer.
I know how to disable them with .isUserInteractionEnabled but I don't know how to enable them again with func textFieldShouldReturn.

Could you show how and where you do it ?

Is your problem that in textFieldShouldReturn you do not reference the textField you need ?

If so, the simplest is to create IBOutlets for the different textField
Code Block
@IBOutlet var text1 : UITextField!
@IBOutlet var text2 : UITextField!
@IBOutlet var text2 : UITextField!

Then in textFieldShouldReturn
Code Block
if textField == textField1 {
textField2.isUserInteractionEnabled = true
textField3.isUserInteractionEnabled = true
}


You can also do it without creating the Outlets, by searching for the textFields.
To be sure you address the correct ones, you could set their tags as 1, 2, 3…

Define the extension:

Code Block
extension UIView {
class func getAllSubviews<T: UIView>(from parentView: UIView) -> [T] {
return parentView.subviews.flatMap { subView -> [T] in
var result = getAllSubviews(from: subView) as [T]
if let view = subView as? T { result.append(view) }
return result
}
}
func get<T: UIView>(all type: T.Type) -> [T] { return UIView.getAllSubviews(from: self) as [T] }
}


And do this in shouldReturn

Code Block
let allTextFields = simpleView.get(all: UITextField.self)
for aTextField in allTextFields {
if aTextField != textField && aTextField.tag >= 1 && aTextField.tag <= 3 { // Be sure to address only the ones you want and not the one called in shouldReturn
aTextField.isUserInteractionEnabled = true
}
}
}



I'm trying to have it like this using your own example, I made comments to explain it.

Code Block
if text1 == textField {
    text2.isUserInteractionEnabled = false. //want text2 to not be used, text1 is only editable
    text1.isUserInteractionEnabled = true
  } else {
    text2.isUserInteractionEnabled = true. //want text2 to be used, text2 is only editable
    text1.isUserInteractionEnabled = false
  }

So what ? Does it work ? If not what is the problem ?
It does not work but I want it to be like what I wrote with the comments.
Your comments are not very clear.
Code Block
if text1 == textField {
text2.isUserInteractionEnabled = false //want text2 to not be used, text1 is only editable
text1.isUserInteractionEnabled = true
} else {
text2.isUserInteractionEnabled = true. //want text2 to be used, text2 is only editable
text1.isUserInteractionEnabled = false
}

line 2: want text2 to not be used, text1 is only editable

what do you get ? Isn't text2 disabled ?

line 5: want text2 to be used, text2 is only editable

is text2 editable. So it can be used ?

Please take time to formulate more precise answers.
  • What do you get exactly ?

  • show the complete real code for the func.

When I click text1 first, you can edit the number but you can also edit text2 which makes the app crash. I'm just trying to have text2 disabled when that happens. When text2 is clicked first I want the exact same thing to happen (have text1 disabled).

When I click text1 first, you can edit the number but you can also edit text2 which makes the app crash. I'm just trying to have text2 disabled when that happens. When text2 is clicked first I want the exact same thing to happen (have text1 disabled).

Where exactly does the app crash ?
What is the crash log ?

You keep repeating what you said before but do not answer questions. And do not show complete code…
So, very hard to help.

You may read those advices on how to best use the forum: https://developer.apple.com/forums/thread/97547
The issue is I want one textfield to be shut off (not clickable) when the other is clicked. Crash happens when you have an empty textField and click on another one.

Fatal error: Unexpectedly found nil while unwrapping an Optional value

Once again, be precise.
Tell where exactly you get the crash.

Does it occur HERE, line 2 or 3 ?
Code Block
if textField == textField1 {
textField2.isUserInteractionEnabled = true
textField3.isUserInteractionEnabled = true
}


If so, you should use optional chaining:
Code Block
if textField == textField1 {
textField2?.isUserInteractionEnabled = true
textField3?.isUserInteractionEnabled = true
}


Fatal error: Unexpectedly found nil while unwrapping an Optional value

I guess the crash occurs somewhere else than shown in this thread.
Generally, you use too many forced-unwrappings (!) and that definitely is causing this sort of issues.

Anyway, please show the code including the exact line which causes this error.
Your right I removed the force unwrappings and fixed the nil issue but even with that the issue still persists. What I need is code for when you click on a textField and it's empty that you can't click on another textField nearby (isUserInteractionEnabled = false) but vice versa with the other textField.

What I need is code

Please show enough code to solve your issue.

Your right I removed the force unwrappings and fixed the nil issue but even with that the issue still persists. What I need is code for when you click on a textField and it's empty that you can't click on another textField nearby (isUserInteractionEnabled = false) but vice versa with the other textField.

What is your spec ?
  • At the beginning, all textFields should be empty.

  • Which one do you enable ?

Then when you edit one, you should keep the other disabled.
To do so:
  • When editing finishes, test the textField :

  • if still empty, then leave others disabled

  • if not empty, then disable it and enable the other

But that may be a very stressful user experience, not being able to edit in any order. User usually do dislike this

Note: don't ask for code, show enough of yours and explain what is not working as expected. Not simply a "it is not working" statement, which does not mean a lot.
This is the code I have its similar to the example in my textFieldShouldReturn function. I don't have the textFields empty as default. The thing is I don't wanna disable it when it's not empty because your right thats bad user experience. With my textFields there is an option to clear and I don't want them editing the other textField when they don't fill in the one they cleared.

Code Block
 if widthPer == textField {
    heightPer.isUserInteractionEnabled = false
    widthPer.isUserInteractionEnabled = true
  } else {
    heightPer.isUserInteractionEnabled = true
    widthPer.isUserInteractionEnabled = false
  }

Accepted Answer

With my textFields there is an option to clear and I don't want them editing the other textField when they don't fill in the one they cleared.

That's new information. It comes piece by piece !

Implement similar code in
Code Block
optional func textFieldShouldClear(_ textField: UITextField) -> Bool

Code Block
if widthPer == textField {
heightPer.isUserInteractionEnabled = false
widthPer.isUserInteractionEnabled = true
} else {
heightPer.isUserInteractionEnabled = true
widthPer.isUserInteractionEnabled = false
}


If that works, don't forget to close the thread on this answer.
Disable TextFields Then Enable Them
 
 
Q