I am using swift. I have a Bar Button Item that I would like to change the Identifier 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 = ?????
}
Unfortunately you can't change the identifier so you have to set the whole bar button item. You have to do the following:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Stop, target: self, action: "startAlarm:")
To make it nicer you can define an array of UIBarButtonSystemItem
s and an index like so:
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, remember to use non-capitalized function and variable names ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With