为什么当我尝试打印按钮标题时使用print(sender.currentTitel)(不起作用)
并且它下面的工作:打印((发送为AnyObject).currentTitle !!)
@IBAction func buttonTapped(_ sender: Any) {
// print here
}
这是由于您在创建IBAction时声明的Any
参考。两种解决方法。
您可以像这样修改您的IBAction:
@IBAction func buttonTapped(_ sender: UIButton) { // print(sender.titleLabel?.text) }
或测试发件人的符合性:
@IBAction func buttonTapped(_ sender: Any) {
if let button = sender as? UIButton {
// print(button.titleLabel?.text)
}
}
如果您的IBAction仅由按钮触发,则解决方案1更好