Reputation: 2730
So far I've been able to get the top-most (the visible) viewController reliably using the following code:
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
//topController.class now contains the name of my top viewController
//I can, for example, check if my contact list view is visible to the user with:
if (topController.class == [ContactViewController class]) {
[viewTitle setText:@"Contacts"];
}
This is pretty easy, and also pretty awesome to use. My problem is that I try to detect if the following class is the top viewController
@interface GalleryViewController : UIViewController <UITextFieldDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
topController.class is now not equal to [GalleryViewController class]
as it would be with all my other viewControllers. Instead, when I use the debugger to check the value it is (_UIModalItemsPresentingViewController *)
with a random memory address (All the other viewControllers show up as (Class)ContactViewController with a random memory address).
Why does this specific class show up with such a weird name, is that because of the definition or is it something else? Also, how can I get the top most viewController properly?
Upvotes: 0
Views: 137
Reputation: 4980
you need to make sure that you actually on the top most window with this snippet
UIWindow *topWindow = [[[UIApplication sharedApplication].windows sortedArrayUsingComparator:^NSComparisonResult(UIWindow *win1, UIWindow *win2) {
return win1.windowLevel - win2.windowLevel;
}] lastObject];
Upvotes: 1