iOS 下面獲取當前棧中的控制器,以方便在子view中做操作
阿新 • • 發佈:2019-01-30
-(UIViewController *)currentViewController{
UIViewController * currVC = nil;
UIViewController * Rootvc = keyWindow.rootViewController ;
do {
if ([Rootvc isKindOfClass:[UINavigationController class]]) {
UINavigationController * nav = (UINavigationController *)Rootvc;
UIViewController * v = [nav.viewControllers lastObject];
currVC = v;
Rootvc = v.presentedViewController;
continue;
}else if([Rootvc isKindOfClass:[UITabBarController class]]){
UITabBarController * tabVC = (UITabBarController *)Rootvc;
currVC = tabVC;
Rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex];
continue;
}
} while (Rootvc!=nil);
return currVC;
}
UIViewController * currVC = nil;
UIViewController * Rootvc = keyWindow.rootViewController ;
do {
if ([Rootvc isKindOfClass:[UINavigationController class]]) {
UINavigationController * nav = (UINavigationController *)Rootvc;
UIViewController * v = [nav.viewControllers lastObject];
currVC = v;
Rootvc = v.presentedViewController;
continue;
}else if([Rootvc isKindOfClass:[UITabBarController class]]){
UITabBarController * tabVC = (UITabBarController *)Rootvc;
currVC = tabVC;
Rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex];
continue;
}
} while (Rootvc!=nil);
return currVC;
}