gpt4 book ai didi

swift - UIBarButton 没有改变

转载 作者:可可西里 更新时间:2023-11-01 00:37:59 27 4
gpt4 key购买 nike

@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")
}

我希望栏按钮更改为暂停符号,但没有成功。它同时打印“按下”和“此处”,但“测试”不起作用。为什么图像没有变化?

最佳答案

你的做法是错误的。

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

阅读此问题了解更多信息。 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.
}

关于swift - UIBarButton 没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27831306/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com