UI 按钮 "if" swift 语句



当我输入代码时,会出现一个错误,上面写着:

错误图片:https://i.stack.imgur.com/ixFaM.png

我该怎么修?

@IBAction func nextQuestionButton(_ sender: Any) {
if (nextQuestionButton.isSelected == true) {

}

在@IBAction方法中使用UIButton,如下

@IBAction func nextQuestionAction(_ sender: UIButton) {
if sender.isSelected {
// returns true value
}
else 
{
// returns false value
}
}

@IBAction func nextQuestionAction(_ sender: Any) {
if ((sender as AnyObject).isSelected) {
// returns true value
}
else 
{
// returns false value
}
}

这是一个误解。

nextQuestionButton而不是按钮,它是连接到按钮的操作。该按钮由sender参数表示。

这种语法使更加清晰

@IBAction func nextQuestionAction(_ sender: UIButton) {
if sender.isSelected {

}
}

最新更新