*Xcode Project Entry 3* Times Tables Quiz - Part 5
This is cheating....a little! The day after my last entry, I managed to fix and completely sort out the app! I spent a good hour or so on it, changing the code so that the pop ups came up for the end of the timer. What a feeling it was to sort it all out!
One of the issues was not having all outlets connected and having some bogus ones, which I removed. That helped.
This bit I commented out as it made no difference:
Self-explanatory really.
All labels and buttons.
One of the issues was not having all outlets connected and having some bogus ones, which I removed. That helped.
This bit I commented out as it made no difference:
//
// func hideKeyboard() {
// textFieldDisplay.resignFirstResponder()
// }
//
// func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// textFieldDisplay.resignFirstResponder()
// return true
// }
I'm not entirely convinced that I need the keyboard code at all...let's see! Actually no real other code there other than the UITextFieldDelegate is conformed to (for the textbox).
So here's what we have -
//GLOBAL VARIABLES
var score = 0
var question = 0
var randomNumber1 = 0
var randomNumber2 = 0
/OUTLETS
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var timerDisplay: UILabel!
@IBOutlet weak var questionNumberLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var messageLabel: UILabel!
@IBOutlet weak var textFieldDisplay: UITextField!
//IB Actions - buttons
@IBAction func submitAnswerButton(_ sender: Any) {
isAnswerCorrect()
newQuestion()
}
@IBAction func newGameButton(_ sender: Any) {
newGame()
resetTimer()
}
//VIEW DID LOAD
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textFieldDisplay.delegate = self
newQuestion()
runTimer()
messageLabel.text = "Good luck!"
}
This is the very start of the app - what comes up at the start.
//TIMER
var seconds = 30
var timer = Timer()
var timeIsRunning = false
func runTimer() {
timeIsRunning = true
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(ViewController.updateTimer)), userInfo: nil, repeats: true)
}
@objc func updateTimer() {
seconds -= 1
timerDisplay.text = String(seconds)
if seconds == 0 {
pauseTimer()
let alert = UIAlertController(title: "Game over!", message: "Your score was \(score) out of \(question - 1)!", preferredStyle: .alert)
let action = UIAlertAction(title: "Click here to dismiss.", style: .default, handler: {
action in self.newGame()
})
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
}
func pauseTimer() {
timer.invalidate()
timeIsRunning = false
}
func resetTimer() {
if timeIsRunning == false {
runTimer()
seconds = 30
timerDisplay.text = String(seconds)
} else if timeIsRunning == true {
pauseTimer()
runTimer()
seconds = 30
timerDisplay.text =
String(seconds)
}
}
Above is everything timer related. The alerts etc are all working now. The question - 1 bit is because the question you are on always gets added to that message and it means you cannot get a 'full score'.
//METHODS
//METHODS
func isAnswerCorrect() {
if timeIsRunning == true {
let answer = randomNumber1 * randomNumber2
if Int(textFieldDisplay.text!) == answer {
score += 1
messageLabel.text = "Correct!"
}
else {
messageLabel.text = "Incorrect! Right answer: \(answer)"
}
}
}
func resetTextBox() {
textFieldDisplay.text = ""
}
func newQuestion() {
randomNumber1 = Int.random(in: 1...12)
randomNumber2 = Int.random(in: 1...12)
questionLabel.text = ("\(randomNumber1) x \(randomNumber2)")
updateScores()
resetTextBox()
}
func updateScores() {
question += 1
questionNumberLabel.text = "Question: \(question)"
scoreLabel.text = "Score: \(score)"
}
func resetScores() {
question = 0
score = 0
messageLabel.text = "Good luck!"
}
func newGame() {
resetScores()
resetTextBox()
newQuestion()
resetTimer()
}
}
And those are all of the methods! I'm proud of how I've used bits from the LettersInWords timer and certain functions. But still it took some logic and trouble shooting.
Estimated time - 1 hour
Next time, I'm going to be working on an Angela course! I need to get back to that as it's been a while. Then I have an idea for more projects!
Comments
Post a Comment