*Xcode Project Entry* Letters in a Word - Part 3
So great progress last time! I got to the point where I need to work on a couple of key things - getting a way to type in the answer is the first priority. One option is to try to add in the return key, this could sort out the issue with using text field. IF NOT, then I will have to add the buttons separately. Then find a way of linking those to a label display...easier said than done!
Start Time - 20:16
First thing's first, let's try out the return key option....
OK tried a couple of things on Stack Overflow but not clear.
Now trying this:
https://www.youtube.com/watch?v=bhp08NX4Pe4
So I've followed various steps but the app is taking an AGE to load. This is not a good sign obviously!
From the tutorial:
A mega marathon session that was partly rewarding - getting to sort out the number issue and entering/getting all the question, score, instruction message matched up...but then the timer. Just goes to show that you CANNOT simply copy and paste code and hope it will work! Tomorrow, I will try that medium website to see if I can just get a 60 second countdown going, then tweak the alert to match it up to when the time is at 0, rather than the question number being on 5. That last part should be ok. It's just getting the timer! Great stuff and much more useful to put the hours in on this at the moment rather than Bob's closures - no offence Bob!
Start Time - 20:16
First thing's first, let's try out the return key option....
OK tried a couple of things on Stack Overflow but not clear.
Now trying this:
https://www.youtube.com/watch?v=bhp08NX4Pe4
So I've followed various steps but the app is taking an AGE to load. This is not a good sign obviously!
From the tutorial:
func hideKeyboard() {
textFieldDisplay.resignFirstResponder()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textFieldDisplay.resignFirstResponder()
return true
}
Right so the app is actually loading now. But keyboard isn't coming up at all...
Now the main screen isn't coming up again - this is very confusing!
Right an issue was that I had an extra text field within another. I've got rid of that, updated the constraints and code. Let's see....
Wow - some time later!
Well I've done it somehow - I'm not convinced it will work on a phone, but I have created code that ties it all together - in terms of the number typed into the text field, the word count of the word and I've changed the button and done another new function or two!
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
//Outlets
@IBOutlet weak var textFieldDisplay: UITextField!
@IBOutlet weak var wordDisplay: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var instructionLabel: UILabel!
//Functions - buttons!
@IBAction func submitAnswerButton(_ sender: Any) {
hideKeyboard()
isAnswerCorrect()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textFieldDisplay.delegate = self
newWord()
}
//Functions and Methods
func hideKeyboard() {
textFieldDisplay.resignFirstResponder()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textFieldDisplay.resignFirstResponder()
return true
}
var total = 0
var score = 0
func newWord() {
let words = ["beagle", "unusual", "loins", "birthday", "category", "Catherine", "Josh", "Clint", "January", "degredation", "happening", "unfair", "whirlwind", "smashing", "turnover", "wooden", "systematic", "fairweather", "panglossian", "pilots", "factory", "radio", "multitude", "plethora", "inebriated", "certainly", "charity", "unfussed", "confidence", "shambles", "oblong", "indecisiveness", "unfamilar", "herald", "furniture", "randomisation", "alliteration", "carpenter", "surgeon", "worldly", "ineptidude", "longitude", "verisimilitude", "fragrant", "evuncular", "backdoor", "banana", "haemoglobin", "unfussy", "necktie", "bowtie", "disregard", "fatigued", "nondescript", "annhilation", "devastation", "happiness", "wonderful", "fabulous", "cucumber"]
let wordcount = words.count
let randomNumber = Int(arc4random_uniform(UInt32(wordcount)))
wordDisplay.text = words[randomNumber]
}
func updateScores() {
total += 1
questionLabel.text = "Question: \(total) of 5"
if total == 5 {
let alert = UIAlertController(title: "Game over!", message: "Your score was \(score) out of 5!", preferredStyle: .alert)
let action = UIAlertAction(title: "Click here to start a new game!", style: .default, handler: {
action in self.submitAnswerButton(action)
})
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
newGame()
}
func newGame() {
total = 0
score = 0
instructionLabel.text = "Enter The Number of Words In The Box Above!"
scoreLabel.text = "Score: 0"
}
func isAnswerCorrect() {
newWord()
updateScores()
let wordDisplayCount = wordDisplay.text?.count
let answer = textFieldDisplay.text
if Int(answer!) == wordDisplayCount {
instructionLabel.text = "Well done! You were right! You score one point!"
score += 1
scoreLabel.text = "Score: \(score)"
} else {
instructionLabel.text = "Unlucky! Try another word!"
}
}
}
I'm sure there was an easier way to do this but it DOES work! OK one issue was the 'Unlucky...' text coming up every time a new game was underway but I fixed that! I used a if total == 1, so the text has to go back to the main instruction. Cool!
So, final step....a timer!!
Not easy to find useful stuff here!
OK I've spent HOURS on finding stuff and there's surprisingly little out there! To make the game more usable, here is my fix:
Get rid of 'out of 5' as the max number of questions.
Instead have it as HOW MANY CAN YOU SCORE IN A MINUTE?! Then the countdown might be easier to use! Let's give that a shot.
So I'm going to have to call it a day. Frustratingly, I thought I nearly had it! But I can't quite get the timer code to work. It's not the end of the world - useful to go through some info.
Just going to have one more look....
Right. It's nearly midnight now! So I'm going to try to use and adapt this code from the website below:
https://medium.com/ios-os-x-development/build-an-stopwatch-with-swift-3-0-c7040818a10f
Stopped at 23:45 (3 hours 29 minutes total!)
A mega marathon session that was partly rewarding - getting to sort out the number issue and entering/getting all the question, score, instruction message matched up...but then the timer. Just goes to show that you CANNOT simply copy and paste code and hope it will work! Tomorrow, I will try that medium website to see if I can just get a 60 second countdown going, then tweak the alert to match it up to when the time is at 0, rather than the question number being on 5. That last part should be ok. It's just getting the timer! Great stuff and much more useful to put the hours in on this at the moment rather than Bob's closures - no offence Bob!
Comments
Post a Comment