简体   繁体   中英

How do I change a play button to a pause button when tapped on by changing its identifier?

I would like to change a bar button item which is initially a play button to a pause button by changing its identifier, but it gives me "UIBarButtonItem" doesn't have a member named "Identifier". How can I achieve this?

Assuming you are using the system generated UIBarButtonItem for the play button you would need to keep strong references to two UIBarButtonItem s, one for the play button, and one for the pause button.

Then when the play button is tapped, remove it from the toolbar and add the pause button in its place. When the pause button is tapped you reverse this process.

For example:

class ViewController: UIViewController {

    @IBOutlet weak var toolbar: UIToolbar!

    var playButton: UIBarButtonItem!
    var pauseButton: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()
        playButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playButtonTapped")
        pauseButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pauseButtonTapped")

        toolbar.items = [playButton];
    }

    func playButtonTapped() {
        toolbar.items = [pauseButton];
    }

    func pauseButtonTapped() {
        toolbar.items = [playButton];
    }

}

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