UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to update buttons status when the user reopens the app? Milestone: Projects 7-9 (Day 41)

Forums > 100 Days of Swift

Hello Everyone!

I have a question about saving the last status of the buttons for a "Hung Person" game. By using UserDefaults I was able to save almost all the status of the user when the game was closed (Screen1 shows the user's game status while playing), as you can see, the user chose 2 wrong letters that were not in the hidden word (M and N, which are in red and disabled) and the user correctly guessed the O, B, C letters (which disappeared from the alphabet and also are disabled).

When the user closes the app and when it is reopened again, I was able to reload almost all the previous data of the last session played, except for the buttons status. You can see this in Screen2. I used UserDefaults to save and load the game. I save the status of the game every time the user taps on any button (trying to guess the hidden word's characters), and I load the data back in viewDidLoad().

The game is made programmatically, I created the buttons using something like the following:

for row in 0..<5 {
        for column in 0..<6 {               
            if counterLetter < 26 {
                let letterButton = UIButton(type: .system)
                letterButton.titleLabel?.font = UIFont.systemFont(ofSize: titleLabelFontSize)
                letterButton.setTitle(englishAlphabet[counterLetter], for: .normal)
                letterButton.isHidden = false
                letterButton.isEnabled = true
                letterButton.alpha = 1
                letterButton.setTitleColor(.red, for: .disabled)
                letterButton.addTarget(self, action: #selector(letterTapped), for: .touchUpInside)
                letterButton.layer.borderWidth = 1
                letterButton.layer.borderColor = UIColor.black.cgColor

                // Button's frame creation
                let frame = CGRect(x: column*widthButton, y: row*heightButton,
                                   width: widthButton, height: heightButton)
                letterButton.frame = frame

                buttonsView.addSubview(letterButton)
                letterButtons.append(letterButton)

                counterLetter += 1
            } else {
                continue
            }
        }
    }

It is in the letterTapped method (inside the #selector(letterTapped) part in the previous code) where I save the user's progress when any alphabet button is tapped. As I said before, I recall the player's last status in viewDidLoad().

I tried to save the buttons status inside the letterTapped method as well, but I haven't been able to save or reload the buttons status as the player's had.

Can you give me please a hand about where I have to use userDefaults to save the last session status of the alphabet buttons, please? So that when the players reopens the app, the screen2 is the same as screen1.

If you need me to share my code, I can do willingly it.

Thanks in advance.

Regards!

3      

In the code shown, you set all buttons as enabled

letterButton.isEnabled = true

You need to set the value restored from UserDefaults.

Also, I don't see you checking the buttons that have already been tapped (which is something you probably have saved to UserDefaults). You need to check those and not add them to the view.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.