【问题标题】:3 differend buttons and actions in 1 button1 个按钮中有 3 个不同的按钮和操作
【发布时间】:2018-12-20 18:55:43
【问题描述】:

我正在尝试开发一款应用简单的游戏。在我的设计中(使用 xcode swift 4.x)我有一个按钮。但是这个按钮实际上是一个3个不同的按钮,每次按下按钮都会改变它的状态和动作。我尝试了一些 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)
            }))
        }
    }
}

【问题讨论】:

    标签: swift xcode button action


    【解决方案1】:

    我认为if btnState == 2 语句位于错误的块中。 另外,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)
            }))
        }
    }
    

    【讨论】:

    • 我在 if 状态下使用此逻辑,但效果不佳。意味着在我的代码中我写错了嗯...感谢您的建议
    • 除非您显示代码,否则没有人可以帮助您。
    【解决方案2】:

    请为不同的事件使用标签。例如,在第一次按下时您设置了 button.tag = 100,第二次按下时设置了标记 200。

    检查按钮动作中的标签:

    @IBAction func btnAction(_ sender: UIButton) {
    
        switch sender.tag {
        case 100:
            //your action
            sender.tag = 200
        case 200:
            //your action
            sender.tag = 300
        case 300:
            //your action
            sender.tag = 100
        default:
            break
        }
    
    }
    

    希望对你有帮助。谢谢

    【讨论】:

    • 只有 1 个按钮,如何分配不同的标签值?
    • 是的,您可以为单个按钮设置不同的标签。标签值将在按钮单击操作时更改
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 2011-12-13
    • 1970-01-01
    • 2011-08-31
    • 2019-05-13
    • 1970-01-01
    相关资源
    最近更新 更多