Swift: Changing Button Bar Element in Code
I use quickly. I have a Bar button element that I would like to change with an ID from Play to Stop in code. Is this possible and how do you do it?
@IBOutlet var StartStopButton: UIBarButtonItem!
@IBAction func StartAlarm(sender: AnyObject) {
onOffIndicator.hidden = false
StartStopButton.Identifier = ?????
}
+3
source to share
1 answer
Unfortunately, you cannot change the ID, so you have to set all the panel items. You must do the following:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Stop, target: self, action: "startAlarm:")
To make it nicer, you can define an array UIBarButtonSystemItem
and an index, for example:
let myArray = [UIBarButtonSystemItem.Start, UIBarButtonSystemItem.Stop]
var index = 0
Then you can do:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: myArray[++index % myArray.count], target: self, action: "startAlarm:")
By the way, don't forget to use non-capitalized function and variable names;)
+6
source to share