Reputation: 379
I've got a basic array of buttons that I'd like to change functionality and look based on whether the user has pressed a "shift"-type separate button.
@IBOutlet var shiftBtn: UIButton!
@IBAction func shiftBtn(_ sender: UIButton) {
changeLookOfsomeBtns() /* ?? */
}
@IBOutlet var someBtns: [UIButton]!
@IBAction func someBtns(_ sender: UIButton) {
if shiftBtn.selected == true {
doSomething()
} else if shiftBtn.selected == false {
doSomethingElse()
}
Is this the right approach? I'm stuck on what technique to apply to change both the function options and image for someBtns.
Thanks!
Upvotes: 0
Views: 36
Reputation: 270770
Here:
if shiftBtn.selected == true {
doSomething()
} else if shiftBtn.selected == false {
doSomethingElse()
}
You seem to be checking a (non-existent) property on a button to decide what to do. In general, you shouldn't make decisions based on a button's property. Instead, you should have a separate property (let's call it shifted
) that indicates whether the shift button is activated or not.
var shifted = false {
didSet {
if shifted {
changeSomeButtonsToShiftedAppearance()
} else {
changeSomeButtonsToNormalAppearance()
}
}
}
@IBAction func someBtns(_ sender: UIButton) {
if shifted {
doSomething()
} else {
doSomethingElse()
}
And you set this property when shift is pressed:
@IBAction func shiftBtn(_ sender: UIButton) {
shifted = !shifted
}
Upvotes: 3