0

Basically, I have a timer set up in an uiapplication to check if the user is idle, and what I want to happen is that if the user is idle long enough, the viewcontroller on top will be the initial log in one.

The timer part works, I've tried it with NSlog.

So basically, I need to find out the current view controller, if it isn't the registration or login view controllers, it should go to the initial log in view controller.

How do I go about getting the current view controller, then switching, in the UIapplication class?

If it's any help, I'm using a navigation controller, not a tab bar controller.

2 Answers2

0

You can retrieve the root view controller of your application by using the sharedApplication singleton:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
UINavigationController *rootNavController = appDelegate.mainWindow.rootViewController;
// Change to what you want your rootViewController
nhgrif
  • 61,578
  • 25
  • 134
  • 173
gsempe
  • 5,371
  • 2
  • 25
  • 29
  • You want `mainWindow`, not `windows`. Also, it might be helpful to point out that to get a reference to the current view controller, he probably needs to call `lastObject` on the navigation controller. – nhgrif Aug 30 '14 at 15:18
  • I fixed it for you now since your fix was still wrong. I still can't upvote this though until you distinguish the difference between the navigation controller and the view controller which is at the top of the navigation stack. – nhgrif Aug 30 '14 at 15:24
  • You miss to fix that rootNavController should be a ref... sad face ;-) – gsempe Aug 30 '14 at 15:24
-2

You can view all your View Controllers on navigationController stack.

See self.navigationController.topViewController and self.navigationController.visibleViewController;

Andrey
  • 30
  • 3
  • And if his app delegate doesn't have a `navigationController` property (which it probably shouldn't)? – nhgrif Aug 30 '14 at 15:17
  • "If it's any help, I'm using a navigation controller, not a tab bar controller." – Andrey Aug 30 '14 at 15:21
  • 1
    I saw and read this part of the question. That he's using a navigation controller does not in itself guarantee that his app delegate class has a `navigationController` property, not does it guarantee that he's properly setting that property even if he's created it. – nhgrif Aug 30 '14 at 15:23