Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
281
86
im trying to save and restore last used view controller when the app is opened, however there is the issue that im using a diffrent swift file for each view controller and im not sure how to save it to user defaults.

this is how i switch between controllers
Code:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let newViewController = storyBoard.instantiateViewController(withIdentifier: "Sample1") as! SampleFile1
            self.present(newViewController, animated: true, completion: nil)

I can save the string objects easily but the last part where it says (withIdentifier: "Sample1") as! SampleFile1, how do i save this SampleFile1 and retrieve it?
 

firewood

macrumors G3
Jul 29, 2003
8,135
1,374
Silicon Valley
Try creating a Dictionary with the ID as key and the class reference as the value. Fill it with all your controllers. Then do a lookup in that Dictionary.
 

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
281
86
Try creating a Dictionary with the ID as key and the class reference as the value. Fill it with all your controllers. Then do a lookup in that Dictionary.

i got it working like that:

save at the end of viewdidload in every class:
Code:
let storyBoard = self.storyboard?.value(forKey: "name") //get storyboard id
        let newViewController = self.restorationIdentifier //get identifier of view controller
        UserDefaults.standard.set(storyBoard, forKey: "storyBoard") // save to user defaults
        UserDefaults.standard.set(newViewController, forKey: "newViewController")

then in AppDelegate:
Code:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

//define variable strings
var storyBoard = ""
        var newViewController = ""
        if let storyBoardLoad = UserDefaults.standard.string(forKey: "storyBoard") {
            storyBoard = storyBoardLoad // restore and set variables
        }
        if let newViewControllerLoad = UserDefaults.standard.string(forKey: "newViewController") {
            newViewController = newViewControllerLoad
        }
       
        if !(storyBoard == "") && !(newViewController == "") { // check that the string is not empty then set main view controller
            let storyboard : UIStoryboard = UIStoryboard(name: storyBoard, bundle: nil)
           
            let vc : UIViewController = storyboard.instantiateViewController(withIdentifier: newViewController)
            self.window?.rootViewController = vc 
        }
       
        return true
}
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93

albebaubles

macrumors 6502a
Feb 9, 2010
636
551
Sierra in view
i got it working like that:

save at the end of viewdidload in every class:
Code:
let storyBoard = self.storyboard?.value(forKey: "name") //get storyboard id
        let newViewController = self.restorationIdentifier //get identifier of view controller
        UserDefaults.standard.set(storyBoard, forKey: "storyBoard") // save to user defaults
        UserDefaults.standard.set(newViewController, forKey: "newViewController")

then in AppDelegate:
Code:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

//define variable strings
var storyBoard = ""
        var newViewController = ""
        if let storyBoardLoad = UserDefaults.standard.string(forKey: "storyBoard") {
            storyBoard = storyBoardLoad // restore and set variables
        }
        if let newViewControllerLoad = UserDefaults.standard.string(forKey: "newViewController") {
            newViewController = newViewControllerLoad
        }
      
        if !(storyBoard == "") && !(newViewController == "") { // check that the string is not empty then set main view controller
            let storyboard : UIStoryboard = UIStoryboard(name: storyBoard, bundle: nil)
          
            let vc : UIViewController = storyboard.instantiateViewController(withIdentifier: newViewController)
            self.window?.rootViewController = vc
        }
      
        return true
}
whoa, arent you only saving the 'id' of the view controller in user defaults? Is that what you intended or were you specifically asking how to serialize the entire VC to user defaults?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.