When I press a specific button (Who's Next) it crashes the app. Though when pressing the other buttons those appears to work as expected. I have created all buttons with the same "method", and a lot of the functions are the same.
I have tried removing the buttons and the reference in the code, adding it again. I have also tried to remove the animation to see if that was the issue.
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
})
})
}
}
I expect the buttons to work the same as the other ones with minor adjustments to the outcome. So when I press the "Who's Next" button it should change the questionText and the helperText