*Xcode Project Entry* Letters in a Word - Part 2
Yes, I'm back to it! Last time in this project, I barely got started as I was figuring out the new layout - mostly where new labels were kept! It's a bit simpler sure, but I didn't see what was wrong with the old one. Something I have learned with Xcode and Swift is that there is no point yearning for a previous feature - it moves on rapidly!
Start Time - 15:20
So, using last time's 'to do list', here's a reminder of what I need to focus on:
On the view of the app
Label to display random words
A number pad to type in answer for how many letters are in the word
A timer - from 5 seconds to 0 each time
A scoring system for tracking
Scoring system to link to timer (potentially!)
The model (code) of the app
An array of words - this is easy enough; can be updated too!
A randomising way so a different word is displayed each time
Functions to calculate the score each time and total score
Functions to update question number etc
Function for the actual stopwatch etc
OK so there are clearly some easier and harder bits here! Let's break down what is needed, in order:
1. Get the label which will display the word each time
2. Add in labels for score and question number
3. Create array of random words
4. Randomised way of displaying words - part of the array and then a function
5. Functions for updating the question number and then start over etc.
6. Timer - look up code for this!
7. Number pad use - again, look up!
These were only in blue so as to separate last time from this time! Looking at the list, I've put in 1. and 2. Before I do anything else I need to get the alignment etc. sorted. Let's do that now - constraint time...
Right constraints done! Just checking it all works...Oh, one addition I need is to add in a couple of pictures of thoughtful beagles...Cath will love that! OK beagles added! All main set up done.
Now on to step 3...
So I've done the word list as an array - easy:
Right, looking good! So I've got the beagles, the title and the button now too. Plus the score and question number labels are under this.
This is all the code including the usual stuff:
Start Time - 15:20
So, using last time's 'to do list', here's a reminder of what I need to focus on:
On the view of the app
Label to display random words
A number pad to type in answer for how many letters are in the word
A timer - from 5 seconds to 0 each time
A scoring system for tracking
Scoring system to link to timer (potentially!)
The model (code) of the app
An array of words - this is easy enough; can be updated too!
A randomising way so a different word is displayed each time
Functions to calculate the score each time and total score
Functions to update question number etc
Function for the actual stopwatch etc
OK so there are clearly some easier and harder bits here! Let's break down what is needed, in order:
1. Get the label which will display the word each time
2. Add in labels for score and question number
3. Create array of random words
4. Randomised way of displaying words - part of the array and then a function
5. Functions for updating the question number and then start over etc.
6. Timer - look up code for this!
7. Number pad use - again, look up!
These were only in blue so as to separate last time from this time! Looking at the list, I've put in 1. and 2. Before I do anything else I need to get the alignment etc. sorted. Let's do that now - constraint time...
Right constraints done! Just checking it all works...Oh, one addition I need is to add in a couple of pictures of thoughtful beagles...Cath will love that! OK beagles added! All main set up done.
Now on to step 3...
So I've done the word list as an array - easy:
var 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"]
Now the random element to it.
Right, looking good! So I've got the beagles, the title and the button now too. Plus the score and question number labels are under this.
This is all the code including the usual stuff:
class ViewController: UIViewController {
@IBOutlet weak var wordDisplay: UILabel!
@IBAction func nextWord(_ sender: Any) {
revealWord()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
revealWord()
}
func revealWord() {
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"]
let wordcount = words.count
let randomNumber = Int(arc4random_uniform(UInt32(wordcount)))
wordDisplay.text = words[randomNumber]
}
}
Yes it works! Couple of things - need to centralise the text box for the words. Also I need to make that bigger.
*Had a pause for 10 or so minutes when Cath came home .
So now - next breakthrough has been creating a function to add to the question number then reset the score after getting to 0. Got it! And I used the alert code from Ray's Bullseye app for it to come up. Neat!
Key thing now is to make sure that the score now gets reset to 0 again!
Yes! Done with a simple reset function. So here's the code for all of that -
func updateScores() {
total += 1
questionLabel.text = "\(total) of 5"
if total == 5 {
let alert = UIAlertController(title: "Game over!", message: "Good try - have another go!", preferredStyle: .alert)
let action = UIAlertAction(title: "Click to start a new game!", style: .default, handler: {
action in self.nextWord(action)
})
alert.addAction(action)
present(alert, animated: true, completion: nil)
resetScore()
}
}
func resetScore() {
total = 0
}
And the updateScore is called every time the button is pressed! Simple and effective! Phew!
Right checking the list...I've actually done the first five! Now it's the number pad before I consider timer..
So there is nothing I can find! How bizarre...So the alternative is to create buttons that have the set numbers in them. Let's just do that for now.
OK I've gotten so far! I've got the text field in there instead, as you can add in text to it. However, there is no option of having a return/done button. So I've found a link here:
https://www.youtube.com/watch?v=iEBF_3rRQPQ
I'll try that after dinner as I've done enough for now.
Finish Time - 17:29 (2 hours 19 minutes total!)
Any more will NOT be good for my eyes! I've achieved so much - the auto layout with constraints, the extra labels and pictures and button, the array with the words, the randomiser function, the updating of the question number...There's loads there! What I need to do next - need to get the return/done key sorted, THEN see if there is a way of communicating that so you can just type in an answer and so then a message comes up with the score updating! If that does work, the last step will be a timer! IF NOT, then I'll do the way of adding in a few buttons which are worth a certain value - though that way will be complex. Any way - great stuff! I can finish this in another few sessions surely!
Comments
Post a Comment