//获取当前控制器
-(UIViewController *)getCurrentVC {
UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
if (!window) {
return nil;
}
UIView *tempView;
for (UIView *subview in window.subviews) {
if ([[subview.classForCoder description] isEqualToString:@"UILayoutContainerView"]) {
tempView = subview;
break;
}
}
if (!tempView) {
tempView = [window.subviews lastObject];
}
id nextResponder = [tempView nextResponder];
while (![nextResponder isKindOfClass:[UIViewController class]] || [nextResponder isKindOfClass:[UINavigationController class]] || [nextResponder isKindOfClass:[UITabBarController class]]) {
tempView = [tempView.subviews firstObject];
if (!tempView) {
return nil;
}
nextResponder = [tempView nextResponder];
}
return (UIViewController *)nextResponder;
}
获取当前控制器
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 开发过程中, 我们往往需要获取当前控制器对象, 但是,一般情况下, 我们都是获取的都是当前的self, 而如果我们...
- 在开发中,我们可能会遇到类似这样的需求,在模态出的登录页面,再模态弹窗,直接进行弹窗是会出现警告的(无作用,) R...
- 经常会用到的两种方式获取当前控制器,一般我们都会把这个方法写在AppDelegate文件里。1、用tabBarCo...