我正在尝试使用setTitle
更改按钮的标题
我没有收到任何错误,我的IBAction
正在工作,但什么也没有发生。
这里我漏掉了什么?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var hourTimerButton: UIButton!
@IBAction func hourTimerButton(_ sender: Any) {
hourTimerButton.setTitle("test", for: [])
print("button")
}
override func viewDidLoad() {
super.viewDidLoad()
hourTimerButton.setTitle("test", for: .normal)
hourTimerButton.setTitle("test", for: [])
}
}
发布于 2017-05-03 11:50:57
尝试像这样将按钮设置为正常状态。
hourTimerButton.setTitle("test", for: .normal)
发布于 2017-05-03 11:57:09
检查你的按钮(HourTimerButton)引用是否正确连接,我现在已经调试了你的代码,一切工作正常。最好是将sender
类型更改为UIButton
,而不是Any
。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var hourTimerButton: UIButton!
@IBAction func hourTimerButton(_ sender: UIBUtton) {
sender.setTitle("test", for: .normal)
print("button")
}
override func viewDidLoad() {
super.viewDidLoad()
hourTimerButton.setTitle("test", for: .normal)
}
}
谢谢。
发布于 2017-05-03 12:22:21
这样试试:
Title1和Title2是UIButton
的两个不同标题
将@IBAction func hourTimerButton(_ sender: Any)
更改为
@IBAction func hourTimerButton(_ sender: AnyObject)
然后执行以下操作:
if (sender.currentTitle == "Title1"){
button.setTitle("Title2", for: .normal)
}
希望它能起作用!
https://stackoverflow.com/questions/43750617
复制相似问题