Unconquered82
Unconquered82

Reputation: 551

Toggle Play/Pause UIBarButtonItem in Toolbar using Swift

I am trying to toggle my button between a play and pause image when I start and stop a ticker using Swift. My code:

import UIKit

class ViewController: UIViewController {

@IBOutlet var btnPlayPause: UIBarButtonItem!
var isPlaying = false
var timer = NSTimer()
var count = 0
@IBOutlet weak var lblTime: UILabel!
@IBOutlet var myToolbar: UIToolbar!




@IBAction func btnPlay(sender: UIBarButtonItem)
    {
   //set the button to animate

    self.myToolbar.setItems([self.btnPlayPause], animated: true)



    if !isPlaying //if the ticker is not ticking
    {
       //change the button to a pause button


        println("worked")//start the ticker

        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)

        isPlaying = true

    }else{ //if the ticker is ticking

        //change the pause button to a play button

        self.btnPlayPause = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: nil)

        //pause the ticker
        timer.invalidate()
        //btnPlayPause.enabled = true
        isPlaying = false
         }

}

    @IBAction func btnReset(sender: UIBarButtonItem)
{
    //reset and restart the ticker
    timer.invalidate()
    count = 0
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)

}


@IBAction func btnStopit(sender: UIBarButtonItem)
{
    //stop and reset the ticker to "0"

    timer.invalidate()
    count = 0
    lblTime.text = String(count)

    isPlaying = false

}

func updateTime()
{
    //displays ticker label with count

    lblTime.text = String(count++)

}

override func viewDidLoad()
{

    super.viewDidLoad()
    let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "btnStopit:")
    self.btnPlayPause = button
}



override func didReceiveMemoryWarning()
{

    super.didReceiveMemoryWarning()

}


}

The Play and all the other buttons on the toolbar are clearing and the toolbar is creating the Pause button alone by itself like this:

Initial View After btnPlayPause clicked

I want to just toggle my Play button with the Pause button without removing any of the other buttons from the toolbar. Is this possible?

If anyone could help me out with this I'd greatly appreciate it!

Thanks

Upvotes: 1

Views: 3591

Answers (3)

Naishta
Naishta

Reputation: 12373

//This code is tested and working with Swift 2

@IBOutlet weak var navigationBar: UINavigationBar!

//playToPause()
@IBAction func playButton(sender: UIBarButtonItem) {

    let newBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pauseButton:")
    navigationBar.topItem?.rightBarButtonItem = newBarButton
}

// pauseToPlay()
@IBAction func pauseButton(sender: UIBarButtonItem){

    let pauseBtnItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playButton:")
    navigationBar.topItem!.rightBarButtonItem = pauseBtnItem
}

Upvotes: 1

Justin Levi Winter
Justin Levi Winter

Reputation: 2767

slightly more dense version:

@IBAction func playPauseToggle(sender: UIBarButtonItem) {
  var barButtonItems = toolBar.items!
  barButtonItems[0] = UIBarButtonItem(barButtonSystemItem: player.rate == 1.0 ? .Pause : .Play,
  target: self, action: "playPauseButtonWasPressed:")
  toolBar.setItems(barButtonItems, animated: true)
}

Upvotes: 1

constantine
constantine

Reputation: 51

Casting your button to a new instance of UIBarButtonItem isn't gonna do the needful. First create an outlet for your toolbar and retrieve the existing items, then change the play button item according to its position on the toolbar.

In my case the toggle button is on the left so I access it with index 0 and replace it with my corresponding toggle. And then calling the setItems() on your toolbar will update your toolbar. Set animation to true for a nice little fade animation.

@IBOutlet weak var toolBar: UIToolbar!

@IBAction func playPauseToggle(sender: UIBarButtonItem) {

    var toggleBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playPauseToggle:")

    if playing {
        player.pause()
        playing = false
    } else {
        player.play()
        toggleBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "playPauseToggle:")
        playing = true
    }

    var items = toolBar.items! 
    items[0] = toggleBtn
    toolBar.setItems(items, animated: true)

}

Upvotes: 5

Related Questions