获取当前屏幕显示的viewcontroller
UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
UIViewController*topVC = appRootVC;
UIViewController*currentVC =nil;
if(topVC.presentedViewController) {
topVC = topVC.presentedViewController;
NSLog(@"数组:%@", topVC.childViewControllers);
NSLog(@"模态出最外层的视图%@", [topVC.childViewControllers lastObject]);
//有模态情况下的根视图
currentVC = [topVC.childViewControllers lastObject];
}else{
//获取非模态情况下的根视图
currentVC = [[ValuePass shareValuePass]getCurrentVC];
}
//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC {
UIViewController*result =nil;
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
if (window.windowLevel != UIWindowLevelNormal) {
NSArray*windows = [[UIApplicationsharedApplication]windows];
for(UIWindow* tmpWininwindows) {
if (tmpWin.windowLevel == UIWindowLevelNormal) {
window = tmpWin;
break;
}
}
}
UIView*frontView = [[windowsubviews]objectAtIndex:0];
idnextResponder = [frontViewnextResponder];
if([nextResponderisKindOfClass:[UIViewControllerclass]]) {
result = nextResponder;
}else{
result = window.rootViewController;
}
/*
* 在此判断返回的视图是不是你的根视图--我的根视图是tabbar
*/
if ([result isKindOfClass:[DWStoreTabBarController class]]) {
DWStoreTabBarController *mainTabBarVC = (DWStoreTabBarController *)result;
result = [mainTabBarVCselectedViewController];
result = [result.childViewControllerslastObject];
}
NSLog(@"非模态视图%@", result);
returnresult;
}