2

In a secondary view controller in my app, I have a table of icons to change to, like the Reddit app Apollo.

In my main view controller, I am using user defaults to save two labels text and the view background color. Without any user default saving at all, when tapping on one of the table rows, I am able to change the app icon freely with these lines:

let appIconService = AppIconService()
appIconService.changeAppIcon(to: .goldAppIcon)

For saving user defaults: When a button is pressed on screen it randomly selects a string from an array and sets one of the two label.text, then randomly selects a UIColor and sets the view.backgroundcolor. I then save those with the "saveLastQuote()" function, and then call the "checkForLastQuote()" function in viewDidLoad() to grab from user defaults:

let defaults = UserDefaults.standard

func saveLastQuote() {
    defaults.set(randomText.text!, forKey: Keys.savedQuote)
    defaults.set(nameText.text!, forKey: Keys.savedName)
    defaults.set(self.view.backgroundColor, forKey: Keys.savedBackgroundColor)
}

func checkForLastQuote() {
    let lastQuote = defaults.value(forKey: Keys.savedQuote) as? String ?? "\"Quote\""
    let lastName = defaults.value(forKey: Keys.savedName) as? String ?? ""
    let lastBackgroundColor = defaults.color(forKey: Keys.savedBackgroundColor)

    randomText.text = lastQuote
    nameText.text = lastName
    self.view.backgroundColor = lastBackgroundColor ?? Colors.black
}

For some reason when I have the saving user defaults code in the main ViewController.swift, when I tap on a row to change the icon on the other screen, I get this dialog box (The screenshot below) indicating to me that it has changed, but when I close the app and reopen, it always stays at its original app icon (Black and white version of the gold one in the screenshot)

Dialog box screenshot

EDIT: When I build and run the app on my device (Which is where I do all my testing for the app), the app's icon DOES change to the new one I clicked in the table view, then continues to not change until I build and run again.

Do I need to save the icon I am changing now since I am using user defaults to append other items? Or possibly grab the changed icon it normally saves by itself from user defaults and set it? I don't know why I would be getting this behavior. Pretty confused about it.

956MB
  • 135
  • 2
  • 12

0 Answers0