2

I have application with navigationController and some root viewController in it's stack. I need a way to reset application's state to default, i.e. when there is only navigationController and it's root viewController from any moment in my application. For example, I make some background data request to backend and get response, which says that I have to logout user immediately. In that moment, let's assume, user has opened a lot of other viewControllers in navigationController stack and maybe some presented viewControllers with their own navigationControllers. So what I need is a way to pull user back from wherever he is to the root viewController of application.

I'm aware of the fact that this is a bad user experience, but it's definitely better than crashing app, and anyway this should be a rare situation.

I don't think that creating a chain reaction between all viewControllers that will one-by-one dismiss them until it reaches my root viewController is a good idea for me. That's too much of identical code in every VC, and I don't want to create subclass of UIViewController and subclass all of my VCs from it to have this done.

Any suggestions?

DemoniacDeath
  • 1,778
  • 3
  • 13
  • 25
  • I ended up using a chain reaction between all viewControllers and having all of my VCs subclassed from custom sublclass of UIViewController with default logic implemented there. I guess there was no way to avoid that. – DemoniacDeath Mar 01 '13 at 22:09

1 Answers1

0

You can use [self.navigationController popToRootViewControllerAnimated:YES]; to go to your main rootview controller from any view of your app.

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
  • Yes, but will it work if I'll have some presented viewControllers? – DemoniacDeath Feb 28 '13 at 09:53
  • if you are adding views in your main view, follow this link, http://stackoverflow.com/questions/2156015/iphone-remove-all-subviews – Baby Groot Feb 28 '13 at 09:58
  • No, I don't add views in my main view. I'm talking about viewControllers hierarchy. Let's say I have 3 viewControllers in my navigation stack and the last one is presenting another modal navigation controller with it's own stack of viewControllers. So when I use popToRootViewControllerAnimated:YES in, let's say, my rootViewController, will it work correct while I still have my presented view controller? – DemoniacDeath Feb 28 '13 at 10:24
  • By the way, I tried this method before animated viewController appearance and it seems to be buggy - current viewController is changed to rootViewController, but navigationItem of previous viewController is still being presented with animation. – DemoniacDeath Feb 28 '13 at 10:27