我正在尝试开发一款简单的应用程序游戏。在我的设计中(使用 xcode swift 4.x)我有一个按钮。但是这个按钮实际上是一个 3 个不同的按钮,每次按下按钮都会改变它的状态和 Action 。我尝试了一些 if - else 语句,但找不到任何方法。任何建议或任何知识如何实现这种代码。不管怎么说,还是要谢谢你。有一个很好的代码。
我写的代码
@IBAction func btnUncoverQuestion(_ sender: RoundedButton) {
btnUncoverQuestion.setTitle(questionArray?[questionNumber].title ?? "No question here", for: .normal)
btnState = btnState + 1
if btnState == 1 {
sender.setTitle("Başlat", for: .normal)
sender.setTitleColor(UIColor.flatWhite, for: .normal)
startTimer()
btnState += 1
if btnState == 2 {
btnState = 0
sender.setTitle("Tamam", for: .normal)
sender.setTitleColor(UIColor.flatWhite, for: .normal)
stopTimer()
let alert = UIAlertController(title: "Sizce cevap doğru mu", message: "Seçimin yap", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Doğru", style: .default, handler: { (trueAction) in
self.playerArray[self.turn].point += 1
self.turn += 1
self.updateQuestionScreen(button: sender)
}))
alert.addAction(UIAlertAction(title: "Yanlış", style: .default, handler: { (wrongAction) in
self.turn += 1
self.updateQuestionScreen(button: sender)
}))
}
}
}
最佳答案
我认为 if btnState == 2
语句在错误的 block 中。
另外,什么是questionNumber
,什么时候递增?它与 self.turn
有什么关系?
你可以试试这个:
@IBAction func btnUncoverQuestion(_ sender: RoundedButton) {
btnUncoverQuestion.setTitle(questionArray?[questionNumber].title ?? "No question here", for: .normal)
btnState = btnState + 1
if btnState == 1 {
sender.setTitle("Başlat", for: .normal)
sender.setTitleColor(UIColor.flatWhite, for: .normal)
startTimer()
} else if btnState == 2 {
btnState = 0
sender.setTitle("Tamam", for: .normal)
sender.setTitleColor(UIColor.flatWhite, for: .normal)
stopTimer()
let alert = UIAlertController(title: "Sizce cevap doğru mu", message: "Seçimin yap", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Doğru", style: .default, handler: { (trueAction) in
self.playerArray[self.turn].point += 1
self.turn += 1
self.updateQuestionScreen(button: sender)
}))
alert.addAction(UIAlertAction(title: "Yanlış", style: .default, handler: { (wrongAction) in
self.turn += 1
self.updateQuestionScreen(button: sender)
}))
}
}
关于swift - 3 个不同的按钮和 1 个按钮中的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51307399/