话不多说,直接上代码吧
@interface UINavigationController (Extension)
+ (UINavigationController *)currentNC;
@end
@implementation UINavigationController (Extension)
+ (UINavigationController *)currentNC
{
if (![[UIApplication sharedApplication].windows.lastObject isKindOfClass:[UIWindow class]]) {
NSAssert(0, @"未获取到导航控制器");
return nil;
}
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
return [self getCurrentNCFrom:rootViewController];
}
//递归
+ (UINavigationController *)getCurrentNCFrom:(UIViewController *)vc
{
if ([vc isKindOfClass:[UITabBarController class]]) {
UINavigationController *nc = ((UITabBarController *)vc).selectedViewController;
return [self getCurrentNCFrom:nc];
}
else if ([vc isKindOfClass:[UINavigationController class]]) {
if (((UINavigationController *)vc).presentedViewController) {
return [self getCurrentNCFrom:((UINavigationController *)vc).presentedViewController];
}
return [self getCurrentNCFrom:((UINavigationController *)vc).topViewController];
}
else if ([vc isKindOfClass:[UIViewController class]]) {
if (vc.presentedViewController) {
return [self getCurrentNCFrom:vc.presentedViewController];
}
else {
return vc.navigationController;
}
}
else {
NSAssert(0, @"未获取到导航控制器");
return nil;
}
}
@end
网友评论