Loading view controller from storyboard objective c - keeps purging content

I have a legacy objective c project that uses a code instantiated navigation controller, that loads other view controllers, no problems. However, due to simplicity I changed one of my tableview controllers to be in a storyboard (inside there is a tablevew with some customs cells - but not important). I load the view controller from the storyboard like this:


UIViewController *searchViewController;

searchViewController = [[UIViewController alloc] init];

searchViewController = [[UIStoryboard storyboardWithName:@"Storyboard1" bundle:nil] instantiateViewControllerWithIdentifier:@"resultViewController"];


[self.navigationController pushViewController:searchViewController animated:YES];


That all works fine.


When the searchViewController is finished doing what it does I pop back to the original view controller with:

[self.navigationController popViewControllerAnimated:YES];


Works great as well.


However, when searchViewController was a traditional NIB/XIB that got pushed onto the navigation controller, I always came back to the same searchViewController whenever the user decided to. With the new (storyboard) approach it seems that a whole new instance that have been created meaning that no results are kept inside. This is of course very irritating if the user decides to go back in and check some other search results.


So why does the two methods differ, I mean technically it should be more or less the same under the hood?

A storyboard segue always creates a new instance of a view controller That is how it works.. If you need to restore some state, you'll have to manually save some information related to state and restore the state in your new instance next time.


You *could* hold the view controller in a property and push the same instance back on the stack if you wanted to, but I wouldn't.

It sounds more like you messed up when you converted to storyboards, and didn't covert that popping of the view controller to an unwind segue.

Because if you just point the segue back to the other view controller, you don't get an unwind segue and instead you get a new view controller pushed onto the stack.

Loading view controller from storyboard objective c - keeps purging content
 
 
Q