I am coding a game where the user can only play if there is an active internet connection. I want to dismiss all ViewControllers and get back to the login screen if the internet status changes. Here’s the hierarchy:
My App Delegate show the LoginViewController by:
self.window.rootViewController = loginViewController;
[self.window makeKeyAndVisible];
Here the user logs in. While logging in I check for connectivity and handle errors. If login is successful loginViewController presents mainViewController using
[self presentModalViewController:self.mainViewController animated:YES];
Then mainViewController presents other controllers, which can present other controllers and so on. The present and dismiss routine is working properly.
Now, if Reachability status changes, I want to display an alert and force the app to get back to the login screen. I know how to listen for these notification and take actions appropriately. It is well documented 🙂
How do I dismiss all view controllers until I land back in the login screen? Do I need to listen to the notification in all view controllers and dismiss them separately? Is there any way of catching the notification in a parent (like the app delegate) and dismissing all view controllers from there (without holding a reference to all of them beforehand)?
Obviously, I’m not using a navigationController so
[self.navigationController popToRootViewControllerAnimated:NO];
is not possible.
Edit: Alternately, I can dismiss and release everything and start from scratch, like if the application is started new. Is this a better approach? How exactly do I do it?
Thank you.
OK I solved it. Simply calling
on the loginViewController dismisses main view controller and all controllers above on the stack.