Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pop to root view controller in app delegate

I have an app that someone logs into and if they launch it after sending it to the background for more than 10 minutes I pop up a nice little "session expired" alert and send them to the root view controller (login page).

Everything works great but I don't know how to pop to the root view controller in my navigation controller stack from the app delegate's applicationWillEnterForeground: method.

I tried saving off the navigationController onto an appDelegate variable but I wonder if the app is in the background for several days, if iOS starts freeing some variables, as I get an error in this method at that time.

Any ideas?

like image 339
Travis M. Avatar asked Dec 11 '22 22:12

Travis M.


2 Answers

If the navigation controller is your application's root view controller, then you can get it like:

UINavigationController *myNavCon = (UINavigationController*)self.window.rootViewController;

where self is in the app delegate

like image 133
Dan F Avatar answered Jan 03 '23 10:01

Dan F


Just to make things more clear one can use following to pop to rootviewcontroller from AppDelegate

UINavigationController *navigationController = (UINavigationController  *)self.window.rootViewController;
[navigationController popToRootViewControllerAnimated:YES];

Here self is app delegate.

like image 44
mhrrt Avatar answered Jan 03 '23 10:01

mhrrt