得到当前活动的controller

UIWindow+PazLabs.h (header file)

#import <UIKit/UIKit.h>

@interface UIWindow (PazLabs)

- (UIViewController *) visibleViewController;

@end

  

UIWindow+PazLabs.m (implementation file)

#import "UIWindow+PazLabs.h"

@implementation UIWindow (PazLabs)

- (UIViewController *)visibleViewController {

    UIViewController *rootViewController = self.rootViewController;

    return [UIWindow getVisibleViewControllerFrom:rootViewController];

}

+ (UIViewController *) getVisibleViewControllerFrom:(UIViewController *) vc {

    if ([vc isKindOfClass:[UINavigationController class]]) {

        return [UIWindow getVisibleViewControllerFrom:[((UINavigationController *) vc) visibleViewController]];

    } else if ([vc isKindOfClass:[UITabBarController class]]) {

        return [UIWindow getVisibleViewControllerFrom:[((UITabBarController *) vc) selectedViewController]];

    } else {

        if (vc.presentedViewController) {

            return [UIWindow getVisibleViewControllerFrom:vc.presentedViewController];

        } else {

            return vc;

        }

    }

}

@end

  

posted @ 2014-08-04 11:26  _还是不懂  阅读(344)  评论(0编辑  收藏  举报