save last used view controller to user defaults

Discussion in 'iOS Programming' started by abcdefg12345, Nov 18, 2017.

  1. abcdefg12345 macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #1
    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?
     
  2. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #2
    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.
     
  3. abcdefg12345 thread starter macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #3
    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
    }
     
  4. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
  5. albebaubles macrumors 6502

    albebaubles

    Joined:
    Feb 9, 2010
    Location:
    low Sierra
    #5
    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?
     

Share This Page