UIBarButton not changing

喜欢而已 提交于 2019-12-24 03:54:07

问题


@IBOutlet weak var playStopButton: UIBarButtonItem!
var playStopArray = [UIBarButtonSystemItem.Pause, UIBarButtonSystemItem.Play]
var index = 0

@IBOutlet weak var image: UIImageView!
@IBAction func playButton(sender: UIBarButtonItem) {
    println("pressed")
    playStopButton = UIBarButtonItem(barButtonSystemItem: playStopArray[index], target: self, action: "startMusic:")
    println("here")
    if index == 0 {
        index = 1
    }
    else {
        index = 0
    }
}
func startMusic() {
    println("test")
}

I expected the bar button to change to the pause symbol, but with no luck. It prints both "pressed" and "here" but "test" does not work. Why is the image not changing?


回答1:


Your approach is wrong.

In the following line,
playStopButton = UIBarButtonItem(barButtonSystemItem: playStopArray[index], target: self, action: "startMusic:")
you are actually creating a new instance of UIBarButtonItem. This button is not actually added into the view. Instead of adding the UIBarButtonItem through Interface Builder. You can create it programmatically.

Read this question for more information. toggle between UIBarButtonSystemItemPlay and UIBarButtonSystemItemPause

var playButton:UIBarButtonItem!
var pauseButton:UIBarButtonItem!

func setup()
{
    playButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "startMusic:")
    pauseButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "stopMusic:")
}

func startMusic:(button : UIBarButtonItem)
{
     self.navigationItem.rightBarButtonItem = pauseButton // Switch to pause.
     //Other code.
}
func stopMusic:(button : UIBarButtonItem)
{
    self.navigationItem.rightBarButtonItem = playButton// Switch to play.
    //Other code.
}



回答2:


Here is my approach in Swift

func configureBars() {
  isPlaying = false
  // UIToolbar
  let pickBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Search,
    target: self,
    action: "pickSong")
  let playBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Play,
    target: self,
    action: "playPause")
  let pauseBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Pause,
    target: self,
    action: "playPause")
  let leftFlexBBI = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace,
    target: nil,
    action: nil)
  let rightFlexBBI = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace,
    target: nil,
    action: nil)
  playItems = [pickBarButtonItem, leftFlexBBI, playBarButtonItem, rightFlexBBI]
  pauseItems = [pickBarButtonItem, leftFlexBBI, pauseBarButtonItem, rightFlexBBI]

  toolbar.items = playItems
}

visit http://www.raywenderlich.com/36475/how-to-make-a-music-visualizer-in-ios or you can check out my Github https://github.com/Charles-Hsu/MusicVisualizer



来源:https://stackoverflow.com/questions/27831306/uibarbutton-not-changing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!