swift - 3 个不同的按钮和 1 个按钮中的操作

标签 swift xcode button action

我正在尝试开发一款简单的应用程序游戏。在我的设计中(使用 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/

相关文章:

swift - fatal error : unexpectedly found nil

swift - 我试图在加载 UITableView 时显示 UIActivityIndi​​catorView

xcode - 我可以使用 Xcode 作为简单的文本编辑器吗?

ios - 我可以在旧项目和新项目之间交换 bundle 标识符吗?

ios - 警告 ITMS-90788 : "Incomplete Document Type Configuration"

c# - 如何将图标集上的区域映射到按钮?

javascript - onclick 事件作用于特定按钮

swift - Playground 时间轴不显示 View

swift - 尝试序列化对象,尽管验证失败

java - 可绘制 == 可绘制?