3

Im trying to return to a specific ViewController in it's current state after going from that ViewController to another using presentViewController.

But when I try to close the other ViewController (with dismissViewController) I get a white screen.

RootViewController *rootViewController 
   = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]
             instantiateViewControllerWithIdentifier:@"RootViewController"];
    [self presentViewController:rootViewController animated:YES completion:nil];

This isn't an option either because that instantiates a new viewcontroller and I want the old ViewController in its current state.

Do I need to pass the RootViewController as an argument when presenting the other ViewController or is there another option to return to the RootViewController in its current state?

abbood
  • 23,101
  • 16
  • 132
  • 246
Mark Molina
  • 5,057
  • 8
  • 41
  • 69
  • instead of showing us the code of something that isn't an option.. can you show us the code that's resulting in the white screen.. and what about it that you want to keep? your example above kinda threw me off – abbood Sep 06 '13 at 13:50
  • Well.. That code above gives me a white screen. And what I want to keep is like everything. Data about the user, downloaded data while using the app & pressed buttton states. All sorts of data that is stored inside that ViewController. – Mark Molina Sep 09 '13 at 08:55

2 Answers2

5

Yes, there is a way to return original screen. I met just like problem but solved it with following code line

[self dismissViewControllerAnimated:YES completion:nil];
user3704354
  • 119
  • 2
  • 5
-1

one way to address this is to avoid having one view controller responsible for presenting and dismissing the other one.. what you can do is create a controller of controllers (give it a singelton method).. and have that object basically keep a reference to any view controller you are interested in maintaining its state. That way you wouldn't have worry about what's going on behind the scenes when you dismiss or present a view controller.

abbood
  • 23,101
  • 16
  • 132
  • 246
  • So basically that MainViewController has the present and dismiss code and when I need to do one of those the other ViewControllers call that method inside the MainViewController? – Mark Molina Sep 09 '13 at 08:53
  • the idea is less about who is responsible for dismissing or presenting views (ie MainViewController or individual viewcontrollers) and more about the MainViewController keeping a reference to all viewcontrollers instantiated.. so that when one is dismissed (regardless how it's dismissed.. ie it dismisses itself or the main view controller dismisses it) the other viewcontroller's state is not affected by it. and that's exactly what you want to achieve. – abbood Sep 09 '13 at 09:07
  • In general this is called [the mediator pattern](http://en.wikipedia.org/wiki/Mediator_pattern). If you look at Carlo Chung's book [Pro Objective-C Design Patterns for iOS](Pro Objective-C Design Patterns for iOS) it discusses how this patterns is applied to an example very similar to yours. – abbood Sep 09 '13 at 09:07
  • but also one thing to keep in mind: in the viewController that is appearing blank.. make sure that you aren't resetting any data in it's `viewDidLoad` or `viewDidAppear`.. (esp viewDidAppear).. b/c even if the main controller has a reference to it.. the data viewController can still erase it's own data.. i hope all this info merits an upvote as well ;) – abbood Sep 09 '13 at 09:15