简体   繁体   中英

changing toolbar item pause/play button ios swift - AVAudioPlayer

Hello i'm a beginner in swift and i'm specifically trying to change the Toolbar item from pause/play and vice versa. This code does the job. However, the icon is not changing from pause to play. What am i doing wrong here?

@IBAction func pausePlayButton(sender: AnyObject) {

    var new_button = UIBarButtonItem()
    if( oceanPlayer.playing ) {

        oceanPlayer.pause()
        // Update the button
        new_button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "Play")
    } else {

        oceanPlayer.play();
        // Create the pause button
        new_button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pause:")
    }

There are several things to do to change a bar button item.

In the first line you are creating a new UIBarButtonItem that is never used because latter you assign different instances. Change it with this:

var new_button : UIBarButtonItem

After you have assigned the button to this var, you have to place it in the navigation bar or toolbar. For example, if this button is to be placed in the right side of a the navigation bar:

self.navigationItem.setRightBarButtonItem( new_button, true )

For a toolbar you have to set a new array of button items using setItems:animated:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM