获取当前最顶层的ViewController
1 - (UIViewController *)topViewController { 2 UIViewController *resultVC; 3 resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]]; 4 while (resultVC.presentedViewController) { 5 resultVC = [self _topViewController:resultVC.presentedViewController]; 6 } 7 return resultVC; 8 } 9 10 - (UIViewController *)_topViewController:(UIViewController *)vc { 11 if ([vc isKindOfClass:[UINavigationController class]]) { 12 return [self _topViewController:[(UINavigationController *)vc topViewController]]; 13 } else if ([vc isKindOfClass:[UITabBarController class]]) { 14 return [self _topViewController:[(UITabBarController *)vc selectedViewController]]; 15 } else { 16 return vc; 17 } 18 return nil; 19 } 20 使用方法 21 22 UIViewController *topmostVC = [self topViewController];
让明天,不后悔今天的所作所为
posted on 2017-08-23 10:13 Sinner_Yun 阅读(249) 评论(0) 编辑 收藏 举报