一,获取最外层的window
+(UIWindow *)getOutWindow() {
id appDelegate = [UIApplication sharedApplication].delegate;
if (appDelegate && [appDelegate respondsToSelector:@selector(window)]) {
return [appDelegate window];
}
NSArray *windows = [UIApplication sharedApplication].windows;
if ([windows count] == 1) {
return [windows firstObject];
}
else {
for (UIWindow *window in windows) {
if (window.windowLevel == UIWindowLevelNormal) {
return window;
}
}
}
return nil;
}
二,获取当前window的控制器VC
+ (UIViewController *)getCurrentActivityViewController {
UIWindow *window = [UIApplication sharedApplication].delegate.window;
NSLog(@"window level: %.0f", window.windowLevel);
if (window.windowLevel != UIWindowLevelNormal) {
NSArray *windows = [[UIApplication sharedApplication] windows];
for (UIWindow * tmpWin in windows) {
if (tmpWin.windowLevel == UIWindowLevelNormal) {
window = tmpWin;
break;
}
}
}
//从根控制器开始查找
UIViewController *rootVC = window.rootViewController;
UIViewController *activityVC = nil;
while (true) {
if ([rootVC isKindOfClass:[UINavigationController class]]) {
activityVC = [(UINavigationController *)rootVC visibleViewController];
} else if ([rootVC isKindOfClass:[UITabBarController class]]) {
activityVC = [(UITabBarController *)rootVC selectedViewController];
} else if (rootVC.presentedViewController) {
activityVC = rootVC.presentedViewController;
} else {
break;
}
rootVC = activityVC;
}
return activityVC;
}
三,根据显示的view找到对应的控制器VC
+ (UIViewController *)getViewControllerWithView:(UIView *)view {
for (UIView* next = [view superview]; next; next = next.superview) {
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)nextResponder;
}
}
return nil;
}
四,判断导航控制器中是否含有某类控制器VC
+ (BOOL)haveSpecialVCWithStack:(UINavigationController *)navi SpecificName:(NSString *)name;
{
for (UIViewController *controller in navi.viewControllers) {
if ([controller isKindOfClass:[NSClassFromString(name) class]]) {
return YES;
}
}
return NO;
}
五,取出导航控制器中的控制器VC
// 获取导航控制器中的FeedBackViewController
for (UIViewController *controller in VC.navigationController.viewControllers) {
if ([controller isKindOfClass:[FeedBackViewController class]]) {
((FeedBackViewController *)controller).inShotImage = image;
[VC.navigationController popViewControllerAnimated:YES];
}else {
}
};