ios获取当前正在运行的视图控制器

2016-10-20  本文已影响50人  陈大帅

1,获取方法:

- (UIViewController *)activityViewController{

UIViewController* activityViewController = nil;

UIWindow *window = [[UIApplication sharedApplication] keyWindow];

if(window.windowLevel != UIWindowLevelNormal){

NSArray *windows = [[UIApplication sharedApplication] windows];

for(UIWindow *tmpWin in windows){

if(tmpWin.windowLevel == UIWindowLevelNormal)

{

window = tmpWin;

break;

}

}

}

NSArray *viewsArray = [window subviews];

if([viewsArray count] > 0){

UIView *frontView = [viewsArray objectAtIndex:0];

id nextResponder = [frontView nextResponder];

if([nextResponder isKindOfClass:[UIViewController class]]){

activityViewController = nextResponder;

}

else{

activityViewController = window.rootViewController;

}

}

return activityViewController;

}


2,调用方法[self activityViewController];

下面的是我使用的一个例子。

//获取视图控制器,进行网页 即刻刷新

BaseNavController *v = (BaseNavController *)[self activityViewController];

//NSLog(@"%@", v.childViewControllers);

KCTabBarViewController *kc = v.childViewControllers[0];

for (int i = 0; i < kc.childViewControllers.count; i++) {

//NSLog(@"for-- %@" , kc.childViewControllers[i]);

}

ShouKuanViewController *vv = kc.childViewControllers[0];

//NSLog(@"request:%@", request)

上一篇下一篇

猜你喜欢

热点阅读