I am saving the storyboard name and the current viewController so that when a user hits "resume" on the home screen (resumeButtonWasPressed in HomeScreenVC), they can immediately be taken back to the view that they were previously looking at. Every time I print the values after setting them in my classes, the correct value gets printed out, such as "Chapter1" "Page2". However, only a percentage of the time does the user get taken to the correct view when they press "resume" on the home screen. The correct values to be saved are printed out in the classes themselves, but when I retrieve the values from UserDefaults in the HomeScreenVC, sometimes the wrong storyboard and viewController get printed out. My code is below.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
_ = UserDefaults.standard
_ = ["strength" : 0]
_ = ["intellect" : 0]
_ = ["charisma" : 0]
_ = ["name" : ""]
_ = ["storyBoard" : "Main"]
_ = ["viewController" : "HomeScreenVC"]
// Override point for customization after application launch.
return true
}
The following is my code for HomeScreenVC with the non-essentials removed:
class HomeScreenVC: UIViewController {
let defaults = UserDefaults.standard
@IBAction func resumeButtonWasPressed(_ sender: Any) {
let sb = defaults.string(forKey: "storyBoard")
let vc = defaults.string(forKey: "viewController")
let storyBoard : UIStoryboard = UIStoryboard(name: sb!, bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: vc!)
self.navigationController?.pushViewController(newViewController, animated: true)
}
override func viewDidAppear(_ animated: Bool) {
let sb = defaults.string(forKey: "storyBoard")
let vc = defaults.string(forKey: "viewController")
print(sb!)
print(vc!)
}
}
This is my infoScreen VC with the non-essentials removed:
class InfoScreenVC: UIViewController {
let defaults = UserDefaults.standard
override func viewDidAppear(_ animated: Bool) {
let storyboard = self.storyboard?.value(forKey: "name")
UserDefaults.standard.set(storyboard, forKey: "storyBoard") // save to user defaults
let newViewController = self.restorationIdentifier
UserDefaults.standard.set(newViewController, forKey: "viewController")
}
}
This is my page1VC with the non-essentials removed:
class Page1VC: UIViewController {
let defaults = UserDefaults.standard
override func viewDidAppear(_ animated: Bool) {
let storyboard = self.storyboard?.value(forKey: "name")
UserDefaults.standard.set(storyboard, forKey: "storyBoard") // save to user defaults
let newViewController = self.restorationIdentifier
UserDefaults.standard.set(newViewController, forKey: "viewController")
}
}
This is my Page2VC with the non-essentials removed:
class Page2VC: UIViewController {
let defaults = UserDefaults.standard
override func viewDidAppear(_ animated: Bool) {
let storyboard = self.storyboard?.value(forKey: "name")
UserDefaults.standard.set(storyboard, forKey: "storyBoard") // save to user defaults
let newViewController = self.restorationIdentifier
UserDefaults.standard.set(newViewController, forKey: "viewController")
}
}