Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the top most UIViewController

If I push view controllers and/or present modal view controllers on a UINavigationController, how can I find out what is the top most UIViewController? Or in my case, I want to know if a certain UITableViewController is the top most or not.

I tried using:

self.navigationController.topViewController == self 

... but this doesn't work. I'm guessing that it's failing because I'm presenting modal view controllers on top of it and that the topViewController only keeps track of which views were pushed on the UINavigationController (as opposed to those that were presented modally).

like image 440
Senseful Avatar asked Nov 01 '10 09:11

Senseful


People also ask

How do I get a topmost view controller?

In addition, you can check for UINavigationController and ask for its topViewController or even check for UITabBarController and ask for selectedViewController . This will get you the view controller that is currently visible to the user.

What is the difference between ViewController and UIViewController?

An UIViewController is just the base class of this already defined "View Controller", you add a viewController as you said, but a viewController has a class associated to it, that can be a UIViewController, UITableViewController, or variations/subclasses.


Video Answer


2 Answers

You want visibleViewController:

The currently visible view can belong either to the view controller at the top of the navigation stack or to a view controller that was presented modally.

like image 136
Adam Ernst Avatar answered Nov 18 '22 18:11

Adam Ernst


NSArray *viewContrlls=[[self navigationController] viewControllers];  [viewContrlls lastObject]; 
like image 35
Swastik Avatar answered Nov 18 '22 18:11

Swastik