美文网首页
ios获取当前正在运行的视图控制器

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

作者: 陈大帅 | 来源:发表于2016-10-20 16:37 被阅读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)

相关文章

网友评论

      本文标题:ios获取当前正在运行的视图控制器

      本文链接:https://www.haomeiwen.com/subject/ffnfuttx.html