I know similar questions have been asked and answered in the past. However, I seem to have trouble with executing a simple act of toggling the left bar button from Play to Pause.
Here is my code:
@IBAction func toggleButton(sender: AnyObject) {
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "someFakeAction"), animated: true)
}
func someFakeAction() {
println("Button pressed")
}
When I tap the Play button, there is a quick fading animation and then the Play button returns. The function someFakeAction is not called. What am I doing wrong?
Try this:
Declare UIBarButtonItem globally so that you can access it later. lets call it
playPauseButtonDeclare a boolean variable globally for keeping track if it is playing or paused, lets call it
isPlaying.From viewDidLoad init
isPlaying = true, and playPauseButton with actionpauseItand set it asleftBarButtonItem.if button is pressed in
pauseItmethod initisPlaying = false, initializeplayPauseButtonwith a actionplayItand set it toleftBarButtonItem.in
playItsetisPlaying = trueagain and init theplayPauseButtonwith actionpauseIt.