Find out, what controller is on the top.
If you want to check, what controller is on the top right now, with this code all what you need is to minimize app and restore it again. And you will see an alert with the title of Controller.
APPDELEGATE
#import "UIViewController+Top.h"
- (void)applicationWillEnterForeground:(UIApplication *)application
{
#ifdef DEBUG
NSString *topController = NSStringFromClass([[UIViewController topViewController] class]);
[[[UIAlertView alloc] initWithTitle:topController
message:nil
delegate:self
cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
#endif
}
#ifdef DEBUG
NSString *topController = NSStringFromClass([[UIViewController topViewController] class]);
[[[UIAlertView alloc] initWithTitle:topController
message:nil
delegate:self
cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
#endif
}
CATEGORY
@interface UIViewController (Top)
+ (UIViewController *)topViewController;
@end
#import "UIViewController+Top.h"
@implementation UIViewController (Top)
+ (UIViewController *)topViewController
{
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
return [rootViewController topVisibleViewController];
}
- (UIViewController *)topVisibleViewController
{
if ([self isKindOfClass:[UITabBarController class]])
{
UITabBarController *tabBarController = (UITabBarController *)self;
return [tabBarController.selectedViewController topVisibleViewController];
}
else if ([self isKindOfClass:[UINavigationController class]])
{
UINavigationController *navigationController = (UINavigationController *)self;
return [navigationController.visibleViewController topVisibleViewController];
}
else if (self.presentedViewController)
{
return [self.presentedViewController topVisibleViewController];
}
else if (self.childViewControllers.count > 0)
{
return [self.childViewControllers.lastObject topVisibleViewController];
}
return self;
}
@end
Комментарии
Отправить комментарий