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
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];
 
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?
 
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?
 
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?

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 ?
 
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.
 
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 ?
Can you describe what you are trying to do?
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.