美文网首页
OC 如何获得当前页面控制器

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

作者: _牙牙 | 来源:发表于2017-05-24 16:58 被阅读0次

话不多说,直接上代码。将代码里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 如何获得当前页面控制器

      本文链接:https://www.haomeiwen.com/subject/rjsoxxtx.html