当按下特定按钮时,它会使应用程序崩溃

时间:2019-06-20 06:47:51

标签: arrays swift xcode button layout

当我按下特定按钮(Who's Next)时,它使应用程序崩溃。尽管按下其他按钮时,这些按钮似乎可以正常工作。我用相同的“方法”创建了所有按钮,并且许多功能都相同。

我尝试删除代码中的按钮和引用,然后再次添加。我也尝试过删除动画,看是否是问题所在。

    import UIKit

    class ViewController: UIViewController {

    // IBOutlets for the different elements on the Main.storyboard.
        @IBOutlet weak var helperText: UILabel!

        @IBOutlet weak var questionText: UITextView!

        @IBOutlet weak var dreamButton: UIButton!

        @IBOutlet weak var reflectButton: UIButton!


        @IBOutlet weak var whosNextButton: UIButton!


// Question pool in an array about the dream questions.

    var dreamData = [
        "What is the most important thing you want to have accomplished within the next five years?",
        "2 dream",
        "3 dream",
        "4 dream",
        "5 dream",
        "6 dream",
        "7 dream",
        "8 dream",
        "9 dream",
        "10 dream",
    ]

// Question pool in an array about reflection questons.

    var reflectionData = [
        "What was the greatest achievement you accomplished the past five years?",
        "2 reflection",
        "3 reflection",
        "4 reflection",
        "5 reflection",
        "6 reflection",
        "7 reflection",
        "8 reflection",
        "9 reflection",
        "10 reflection",
    ]


// Name Pool for the Who's Next Button/function

    var namePoolData = [
        "Mathias",
        "Kasper",
        "Jason",
        "Oliver",
        "Jacob",
        "Mie",
        "Jenni",
        "Angela",
        "Mille",
        "Valerija"

    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        dreamButton.layer.cornerRadius = 15
        reflectButton.layer.cornerRadius = 15
        whosNextButton.layer.cornerRadius = 15
    }


//IBActions - Buttons on the Main.storyboard (Dream, Reflect, Who's Next?)


    @IBAction func dreamButton(_ sender: UIButton) {
        self.questionText.text = self.dreamData.randomElement()
        self.helperText.text = "Question about dreams!"
        UIButton.animate(withDuration: 0.2,
                         animations: {
                            sender.transform = CGAffineTransform(scaleX: 0.975, y: 0.96)
        },
                         completion: { finish in
                            UIButton.animate(withDuration: 0.2, animations: {
                                sender.transform = CGAffineTransform.identity
                            })
        })
    }


    @IBAction func reflectButton(_ sender: UIButton) {

        self.questionText.text = self.reflectionData.randomElement()
        self.helperText.text = "Question about reflections!"
        UIButton.animate(withDuration: 0.2,
                         animations: {
                            sender.transform = CGAffineTransform(scaleX: 0.975, y: 0.96)
        },
                         completion: { finish in
                            UIButton.animate(withDuration: 0.2, animations: {
                                sender.transform = CGAffineTransform.identity
                            })
        })
    }


    @IBAction func whosNextButton(_ sender: UIButton) {
        self.questionText.text = self.namePoolData.randomElement()
        self.helperText.text = "It is your turn!"
        UIButton.animate(withDuration: 0.2,
                         animations: {
                            sender.transform = CGAffineTransform(scaleX: 0.975, y: 0.96)
        },
                         completion: { finish in
                            UIButton.animate(withDuration: 0.2, animations: {
                                sender.transform = CGAffineTransform.identity
                            })
        })
    }


}

我希望这些按钮的工作方式与其他按钮相同,但会对结果进行细微调整。因此,当我按下“谁的下一步”按钮时,应该更改问题文本和帮助文本

0 个答案:

没有答案