UIBarButton没有改变

UIBarButton没有改变

本文介绍了UIBarButton没有改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@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?

推荐答案

您的方法是错误的.

在下面的行中,
playStopButton = UIBarButtonItem(barButtonSystemItem: playStopArray[index], target: self, action: "startMusic:")
您实际上是在创建UIBarButtonItem的新实例.该按钮实际上并未添加到视图中.而不是通过Interface Builder添加UIBarButtonItem.您可以以编程方式创建它.

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.

阅读此问题以获取更多信息.在UIBarButtonSystemItemPlay和UIBarButtonSystemItemPause之间切换

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.
}

这篇关于UIBarButton没有改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:55