Angela Yu Xcode 12 Course - Part 10 (lectures 113 to 131)

Right so next project! This was one of my favourite ones from before as there was a lot of logic but it was all very accessible too. With this idea, I could make limitless options and stories. Anyway, let's go!

Start Time - 18:19



Right so that's the basic look of the app.




And there's the decision tree for all the possibilities. Makes sense.

OK project file downloaded!

Next bit - tag values of the two buttons. Yes there are two options each time.


Right, after some fiddly code, here's what I have -

if sender.tag == 1 && storyIndex == 1 {
            storyTextView.text = story3
            topButton.setTitle(answer3a, for: .normal)
            bottomButton.setTitle(answer3b, for: .normal)
            storyIndex = 3
            
        } else if sender.tag == 1 && storyIndex == 3 {
            storyTextView.text = story6
            topButton.setTitle("", for: .normal)
            bottomButton.setTitle("", for: .normal)
            storyIndex = 1
            
        } else if sender.tag == 2 && storyIndex == 1 {
            storyTextView.text = story2
            topButton.setTitle(answer2a, for: .normal)
            bottomButton.setTitle(answer2b, for: .normal)
            storyIndex = 2
            
        } else if sender.tag == 2 && storyIndex == 2ge {
            storyTextView.text = story4
            topButton.setTitle("", for: .normal)
            bottomButton.setTitle("", for: .normal)
            storyIndex = 1
            
        } else if sender.tag == 1 && storyIndex == 2 {
           storyIndex = 3
        
        } else if sender.tag == 2 && storyIndex == 3 {
            storyTextView.text = story5
            topButton.setTitle("", for: .normal)
            bottomButton.setTitle("", for: .normal)
            storyIndex = 1

    }


I know I don't need all of the storyIndex bits. And I need to have a 'reset' after the end bits - at stories 4, 5 and 6.

Hang on, yes I can do that! I've found 'isHidden' which need to be true for those.

And I've created a button to start over! Cool!

    @IBAction func resetGame(_ sender: UIButton) {
        
        storyIndex = 1
        storyTextView.text = story1
        topButton.setTitle(answer1a, for: .normal)
        bottomButton.setTitle(answer1b, for: .normal)
        
        showButtons()
    }
    
    func showButtons() {
        topButton.isHidden = false
        bottomButton.isHidden = false
    }

    

Let's see the solution!

OK a couple of tweaks but not many!

    @IBOutlet weak var topButton: UIButton!         // Has TAG = 1
    @IBOutlet weak var bottomButton: UIButton!      // Has TAG = 2
    @IBOutlet weak var storyTextView: UILabel!
    @IBOutlet weak var restartGame: UIButton!
    
    // TODO Step 5: Initialise instance variables here
    
    var storyIndex = 1
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        storyTextView.text = story1
        
        topButton.setTitle(answer1a, for: .normal)
        bottomButton.setTitle(answer1b, for: .normal)
        
        restartGame.isHidden = true
        // TODO Step 3: Set the text for the storyTextView, topButton, bottomButton, and to T1_Story, T1_Ans1, and T1_Ans2
        
        
    }

    
    // User presses one of the buttons
    @IBAction func buttonPressed(_ sender: UIButton) {
    
        // TODO Step 4: Write an IF-Statement to update the views
        
        if sender.tag == 1 && storyIndex == 1 || storyIndex == 2 {
            storyTextView.text = story3
            topButton.setTitle(answer3a, for: .normal)
            bottomButton.setTitle(answer3b, for: .normal)
            storyIndex = 3
            
        } else if sender.tag == 1 && storyIndex == 3 {
            storyTextView.text = story6
            topButton.isHidden = true
            bottomButton.isHidden = true
            storyIndex = 6
            
        } else if sender.tag == 2 && storyIndex == 1 {
            storyTextView.text = story2
            topButton.setTitle(answer2a, for: .normal)
            bottomButton.setTitle(answer2b, for: .normal)
            storyIndex = 2
            
        } else if sender.tag == 2 && storyIndex == 2 {
            storyTextView.text = story4
            topButton.isHidden = true
            bottomButton.isHidden = true
            storyIndex = 4
            
        } else if sender.tag == 2 && storyIndex == 3 {
            storyTextView.text = story5
            topButton.isHidden = true
            bottomButton.isHidden = true
            storyIndex = 5
    }
        
        // TODO Step 6: Modify the IF-Statement to complete the story
        
        if storyIndex == 4 || storyIndex == 5 || storyIndex == 6 {
            
            restartGame.isHidden = false
            
        }
    
    }
    
    @IBAction func resetGame(_ sender: UIButton) {
        
        storyIndex = 1
        storyTextView.text = story1
        topButton.setTitle(answer1a, for: .normal)
        bottomButton.setTitle(answer1b, for: .normal)
        
        showButtons()
    }
    
    func showButtons() {
        topButton.isHidden = false
        bottomButton.isHidden = false
        restartGame.isHidden = true
    }
    

}





ACTUALLY - final alterations to stop repeating code. A new function for resetting, which means the initial bit has 'resetScores' and I've got the resetting buttons there too. Nice!


override func viewDidLoad() {
        super.viewDidLoad()
        
      resetScores()

        // TODO Step 3: Set the text for the storyTextView, topButton, bottomButton, and to T1_Story, T1_Ans1, and T1_Ans2
        
        
    }

    
    // User presses one of the buttons
    @IBAction func buttonPressed(_ sender: UIButton) {
    
        // TODO Step 4: Write an IF-Statement to update the views
        
        if sender.tag == 1 && storyIndex == 1 || storyIndex == 2 {
            storyTextView.text = story3
            topButton.setTitle(answer3a, for: .normal)
            bottomButton.setTitle(answer3b, for: .normal)
            storyIndex = 3
            
        } else if sender.tag == 1 && storyIndex == 3 {
            storyTextView.text = story6
            topButton.isHidden = true
            bottomButton.isHidden = true
            storyIndex = 6
            
        } else if sender.tag == 2 && storyIndex == 1 {
            storyTextView.text = story2
            topButton.setTitle(answer2a, for: .normal)
            bottomButton.setTitle(answer2b, for: .normal)
            storyIndex = 2
            
        } else if sender.tag == 2 && storyIndex == 2 {
            storyTextView.text = story4
            topButton.isHidden = true
            bottomButton.isHidden = true
            storyIndex = 4
            
        } else if sender.tag == 2 && storyIndex == 3 {
            storyTextView.text = story5
            topButton.isHidden = true
            bottomButton.isHidden = true
            storyIndex = 5
    }
        
        // TODO Step 6: Modify the IF-Statement to complete the story
        
        if storyIndex == 4 || storyIndex == 5 || storyIndex == 6 {
            
            restartGame.isHidden = false
            
        }
    
    }
    
    @IBAction func resetGame(_ sender: UIButton) {
        
        resetScores()
        
    }
    
    func resetScores() {
        
        storyIndex = 1
        storyTextView.text = story1
        topButton.setTitle(answer1a, for: .normal)
        bottomButton.setTitle(answer1b, for: .normal)
        
        showButtons()
    }
    func showButtons() {
        topButton.isHidden = false
        bottomButton.isHidden = false
        restartGame.isHidden = true
    }
    
    


}

OK - one more thing I want to do is to create a class which has the different values. Then that means I can create the instance variables without all the text in. Let's have a go at that....

YES that works! And that is now a lot neater!!

Right, so project complete and lots of successes. Going straight onto the next course and will get that done!

Auto Layout

This is something I want a bit more help with so useful to go through!

OK key point about constraints - do these from the edges of the screen! Get rid of the tick box and choose view - which will do it from the edge of the screen. 

But the issue here is that on different screens, the square will look very squashed etc. 

Another option is to choose the size, then set horizontally and vertically in the centre...

Some good stuff with creating a view then fixing size etc. 

OK this makes more sense - getting the size sorted, then the positions. Using the top, middle and bottom views is a really good idea. 

Challenge time!

OK nailed that! Easier than when I did it a few months ago to be sure. Key thing was to select "EQUAL HEIGHTS" for the top and bottom views. Cool.

Stack views

I've done these before with Sandra. 

OK this is clever stuff. Definitely the way to go for many buttons!

To be honest, I will need to revisit this when I'm actually doing stack views for real.

https://www.udemy.com/ios-12-app-development-bootcamp/learn/v4/t/lecture/10929106?start=0


Finish Time - 20:35 (Total time 2 hours 16 minutes)

Wow - what a session! Lots of useful code with the Destini app, including my idea of having the classes used and the functions etc. Auto constraints made more sense as did the stack views. Great stuff! Next time, it's intermediate Swift, which will be a useful recap. Then I'm not TOO far away where I was with the other course!

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)