OC 如何获得当前页面控制器

话不多说,直接上代码。将代码里WYTabBarController换成你自己的根视图是tabbar


#pragma mark 判断当前页面

- (UIViewController *)presentingVC{

UIWindow * window = [[UIApplication sharedApplication] keyWindow];

if (window.windowLevel != UIWindowLevelNormal){

NSArray *windows = [[UIApplication sharedApplication] windows];

for(UIWindow * tmpWin in windows){

if (tmpWin.windowLevel == UIWindowLevelNormal){

window = tmpWin;

break;

}

}

}

UIViewController *result = window.rootViewController;

while (result.presentedViewController) {

result = result.presentedViewController;

}

if ([result isKindOfClass:[WYTabBarController class]]) {

result = [(WYTabBarController *)result selectedViewController];

}

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

result = [(UINavigationController *)result topViewController];

}

return result;

}

你可能感兴趣的:(OC 如何获得当前页面控制器)