Reputation: 119
Case: I have 2 UISwitches - Switch1 and Switch2. Switch1 controls the visbility of a UILabel in my scene. Switch2 once activated turns Switch1 off as well as its visbility.
Problem: After Switch2 is activated Switch1 turns off as well as disappears however my UILabel is still visible in my scene.
switch1.addTarget(self, action: #selector(switch1Action), for: UIControl.Event.valueChanged)
switch2.addTarget(self, action: #selector(switch2Action), for: UIControl.Event.valueChanged)
@objc func switch1Action(switch1: UISwitch) {
if switch1.isOn {
lockedMessage.isHidden = false
}
if !switch1.isOn {
lockedMessage.isHidden = true
}
}
@objc func switch2Action(switch2: UISwitch) {
if switch2.isOn {
switch1.isOn = false
switch1.isHidden = true
}
Many thanks! :)
Upvotes: 0
Views: 64
Reputation: 119128
This is correct and desired behavior. Since you explicitly changed the value, it is up to you to decide how to handle the changed value.
The reason for this is because it is not uncommon to explicitly change the value of control after being notified of its value being changed through user interaction. If the explicit state change caused the event to fire again, you would end up in an infinite loop. "@rmaddy"
Deprecated (since the original question updated):
You (probably accidentally) set a unrelated condition for hiding the label
. (What is moveWall
?).
Try this instead:
@objc func switch1Action(switch1: UISwitch) {
lockedMessage.isHidden = !switch1.isOn
,,,
}
Upvotes: 1
Reputation: 1013
If I understand your issue correctly, then it seems you want lockedMessage to be hidden if switch2 is on as well. If this is the case – you can change the visibility of the lockedMessage within your function switch2Action.
@objc func switch2Action(switch2: UISwitch) {
if switch2.isOn {
switch1.isOn = false
switch1.isHidden = true
lockedMessage.isHidden = true
}
Upvotes: 1
Reputation: 1135
Setting switch1.isOn
programmatically will not trigger the switch1Action
. You need to hide the message label from switch2Action
.
For reference see the documentation:
Setting the switch to either position does not result in an action message being sent.
Upvotes: 0