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.
if widthPer == textField {
heightPer.isUserInteractionEnabled = false
widthPer.isUserInteractionEnabled = true
} else {
heightPer.isUserInteractionEnabled = true
widthPer.isUserInteractionEnabled = false
}
Post
Replies
Boosts
Views
Activity
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.
I found my mistake I was trying to transfer numText instead of originalNum.
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
// Make sure the user entered a number
let num = Double(textField.text!)
originalNum = Int(textField.text!)!
if num! < 0 {
textField.text = "0"
} else if num! > 1000 {
textField.text = "1000"
} else if num! >= 0 || num! <= 1000 {
textField.text = String(Int(num!))
}
return true
}
let numText = originalNum
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
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).
It does not work but I want it to be like what I wrote with the comments.
I'm trying to have it like this using your own example, I made comments to explain it.
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	
}
Thank you it was that all along.
When I print numText it shows its exactly what the user entered and thats the issue.
line 17 and line 50, I just want have what the user entered become 90.
I defined it as originalNum = Int(textField.text!)! in another function. What does not work is what the green comments suggest like it makes it over 90 when its supposed to be 90 because of numText = 90
Thanks again it was leaving the fields empty.
Alright I was able to fix it with your suggestion.
I don't mean making the slider disappear, I mean making the shape it controls disappear because 0 is nothing, I will try your solution.
I will give you what I do for one of them since the rest are the same, for ot I have one variable and four outlets, in a class called PoVC
var otPerNum: Int = 0		 //number I use in the textField
@IBOutlet weak var otStack: UIStackView!	 //I use this to get the maximum value and set the value for the slider in the beginning.
otSlider.maximumValue = Float(otStack.frame.width)
otSlider.value = Float(otStack.frame.width) * 0.9
@IBOutlet weak var otWidth: NSLayoutConstraint!			//Use this to set what the measurement for how wide the shape is and control it using otWidth.constant.
@IBOutlet weak var otPer: UITextField!		 //textField I use
@IBOutlet weak var otSlider: UISlider!			//This is the slider that controls the otWidth.constant and sends the number to the otPer.