Change view controller

Discussion in 'iOS Programming' started by abcdefg12345, Apr 14, 2016.

  1. abcdefg12345 macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #1
    hey all

    I'm trying to build an app with multiple view controllers and story boards, however when changing controllers my story board clearing all values like if i just launched the app

    thats my code to change views is there a way to prevent it from resetting everything every time i switch view controllers?

    Code:
    NSString * storyboardName = @"Main";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"MainView"];
    [[UIApplication sharedApplication].keyWindow setRootViewController:vc];
    [self.view.window.rootViewController presentViewController:vc animated:YES completion:nil];
     
  2. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #2
    When you instantiate or create your new view controllers, are creating them from scratch, nothing from old screens is retained. You need a way to hold onto the data or pass the view controllers new data so they can populate it as you like. Can you explain how the data is getting onto the view controller in the first place?
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    Your code does exactly what you say it does. It resets your UI as if the app is just started. That's what setRootViewController does. Normally that method is only called one time when your app launches.

    iOS apps use a root container view controller. This is usually a navigation controller, tab controller, master detail controller or similar. Those controllers have APIs to show view controllers. Also, the story board can be set up to navigate among view controllers by the user with no code using segues.

    Which container view controller are you using?
     
  4. abcdefg12345 thread starter macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #4
    I'm using the normal view controller UIViewController, should i have a method that gets called every time i switch controller which saves and restores the values of the view controllers ?
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    Think again. Are you using UINavigationController or UITabBarController?

    Whether you need to save and restore values depends on what your app does. It is certainly common that a new view controller receives a data object that describes the data that it displays. It's also common that if the user changes something that data has to be preserved somewhere.

    It's not common that an app has only a single view controller at a time that gets replaced, like you show in your code. If you really only have one view controller in the app then why not just reset it to its base state rather than replace it?

    Maybe you should tell us more about what your app does.
     
  6. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #6
    Can you describe what you are trying to do?
     
  7. bjet767 macrumors 6502a

    Joined:
    Oct 2, 2010
    #7
    Normally you would use a single "root controller" as an entry into your app and then use some form navigation to go from one UIViewController to anther. What it sounds like you are trying to do is code all the things the storyboard can do for you.

    After starting with a template (Single View, Tab, Master...) normally you would drop a UIViewController onto your storyboard and the build segues or navigation to them from other view controllers or objects, letting the Apple API do the work for you. Each UIViewController you drop on the story board needs a custom UIViewController object allowing you to write your interactions with the user.

    Read up on good iOS practices to help you figure this out. There are many sources and examples online.
     

Share This Page