Ray Wenderlich Course Part 5 (up to lesson 45)

After a couple of brief-ish entries today, I am going for the hat-trick! During each of the last two, I was able to apply my understanding really well - something I used to be unconfident about. Also, I've picked up some useful tips and ideas along the way. E.g. the abs method was such a good idea and I had no idea about that before. The Bullseye app is coming along well and I have had lots of ideas about it while shaping it alongside Ray's tutorials. Anyway, let's go!

*Also, I have found that my screenshots have not been saved after reviewing the blogs :( That is a serious blow as they now will be out of context! Will address that another time and try to avoid any screenshots for now.

Challenge - Local vs Instance Variables

So the challenge is to look at the 'showAlert' method and make a note of which are local and which are instance.

OK - easy enough I think!

Local - difference, points, title, message, alert, action

Instance - currentValue, targetValue, score, roundNumber


So the ones above are local because they are created within the method - let/var used.

They are also local because they are only used within the confines of the showAlert method. Think of local for temporary storage. I know Instance as 'global' - same difference!

Closures

One of the issues with the bullseye app is that the round updates before you could have properly read the message...

let action = UIAlertAction(title: "Next Round!", style: .default, handler: {
            action in self.updateScores()
        

        })

I've never quite got Closures and still don't! But this is cool - it basically means that the new round label and next target are delayed until the button is pressed. So THEN the updateScores method is called. 

Challenge - Starting Over

Should be easy! Let's do this!

  @IBAction func startOverButton(_ sender: UIButton) {
        
        roundNumber = 1
        score = 0
        
        updateScores()
        
    }

Excellent - Ray actually did it a more long-winded way then tweaked it so it's more like mine!

Adding Extra Screens

So in an app you can have multiple screens - for the Bullseye app, there is the info button, which tells us about the game. In other apps, there could be lots when a button is pressed to show different views. Makes sense. 

Segues - transition from one screen to another. This was a bit of a challenge as I had to choose certain options for this to work, determined not to do Ray's long-winded way of connecting outlets/actions! Anyway, it works! The dismiss method is a good one!

@IBAction func closeButton() {
        dismiss(animated: true, completion: nil)
    }

I also had to change the settings on the Identity Inspector for the AboutViewController screen. 

Challenge - adding a new screen

So time to apply what I have learned!

Yep - all good! Basically I added another view controller scene, did the code for the exit button, added the text plus a picture. All no problem! The only thing again was the action button for exit - I had to choose the option for the view controller I am in, with Any, none for sender etc. 

OK, this gives a bit more detail from one of Ray's answers...

Yep! You have three options - omit the parameter (if you don't care about it), specify the parameter as Any (not really recommended unless you want to reuse the same callback for multiple control types), or specify the parameter as the type of the control (i.e. UIButton). 

I've just been trying to add in another aspect to the app - if the roundNumber gets to 5, that the message changes etc. However, it just didn't seem to work - too many local variables for it to work! Something to think about another time when I'm a bit more skilled! Anyway, some good other key takeaways from these lessons:

  • Segues - going back and forward between ViewController scenes
  • Adding in my own picture
  • The option needed for having another ViewController screen working
  • Using a closure in practice (still not exactly sure what these are...)


Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)