简体   繁体   English

UIBarButton没有改变

[英]UIBarButton not changing

@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? 为什么图像没有变化?

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 . 您实际上是在创建UIBarButtonItem的新实例。 This button is not actually added into the view. 该按钮实际上并未添加到视图中。 Instead of adding the UIBarButtonItem through Interface Builder. 而不是通过Interface Builder添加UIBarButtonItem You can create it programmatically. 您可以以编程方式创建它。

Read this question for more information. 阅读此问题以获取更多信息。 toggle between UIBarButtonSystemItemPlay and UIBarButtonSystemItemPause 在UIBarButtonSystemItemPlay和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.
}

Here is my approach in Swift 这是我在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 请访问http://www.raywenderlich.com/36475/how-to-make-a-music-visualizer-in-ios ,也可以查看我的Github https://github.com/Charles-Hsu/MusicVisualizer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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