.h 中暴露接口
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
UIViewController * currentVC();
.m 中實現(xiàn)
#import "HelpMethodManager.h"
UIViewController * findBestVC(UIViewController *vc) {
if (vc.presentedViewController) {
return findBestVC(vc.presentedViewController);
} else if ([vc isKindOfClass:[UISplitViewController class]]) {
UISplitViewController *sp = (UISplitViewController *)vc;
return sp.viewControllers.count > 0 ? findBestVC(sp.viewControllers.lastObject) : vc;
} else if ([vc isKindOfClass:[UINavigationController class]]) {
UINavigationController *na = (UINavigationController *)vc;
return na.viewControllers.count > 0 ? findBestVC(na.topViewController) : vc;
} else if ([vc isKindOfClass:[UITabBarController class]]) {
UITabBarController *ta = (UITabBarController *)vc;
return ta.viewControllers.count > 0 ? findBestVC(ta.selectedViewController) : vc;
}
return vc;
}
UIViewController * currentVC() {
UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
return findBestVC(rootVC);
}
如果頁面跳轉(zhuǎn)掘剪,currentVC() 的值也將改變腻格,他永遠獲取當前頁面的控制器画拾。