Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i get current active view controller?

I'm having this issue (error is reported by Flurry analytics from some users, but I cannot reproduce it neither on my device nor simulator):

NSInvalidArgumentException: Application tried to present modally an active controller <CityViewController: 0x361b20>.

Do you know anything about this error? Has it happened to you?

Application has three hierachically sorted views: master view controller with list of locations and detail view (the CityViewController mentioned in error message) is automatically presented when user is at one of known locations. Whenever location significantly changes, detail view is dismissed first before opening new location to prevent this. However, it apparently doesn't help.

CityViewController can also open SMS composer as its child.

like image 534
igraczech Avatar asked Mar 26 '13 09:03

igraczech


People also ask

How can I tell if UIViewController is visible?

The view's window property is non-nil if a view is currently visible, so check the main view in the view controller: Invoking the view method causes the view to load (if it is not loaded) which is unnecessary and may be undesirable. It would be better to check first to see if it is already loaded.

How do I get navigation controller ViewController?

The root view controller is simply the view controller that sits at the bottom of the navigation stack. You can access the navigation controller's array of view controllers through its viewControllers property. To access the root view controller, we ask for the first item of the array of view controllers.

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.


2 Answers

To get the currently visible view you can use:

[self.navigationController visibleViewController];
like image 143
vishal rajput Avatar answered Sep 21 '22 03:09

vishal rajput


So the reply to original question: How do i get current active view controller?

The answer lies in [self.navigationController presentedViewController] instead of topViewController or anything else.

like image 28
igraczech Avatar answered Sep 20 '22 03:09

igraczech