IOS获取顶层的viewcontroller
2016-08-04 15:05
477 查看
1 下面的方法 是在假设window的rootViewcontroller有VC的情况的获取方式, rootViewcontroller 为空的app 我没见过,如果你的是的,自己再添加代码吧 这是去高德面试的时候,被问的一个问题,当时不会,这种问题,怎么说呢,只要给我一台互联网的电脑,我就能解决,我不可能知道所有的技巧,但是我有解决问题的能力
- (UIViewController *)appRootViewController
{
UIWindow *keyWindow = [UIApplication
sharedApplication].keyWindow;
UIViewController *vc = keyWindow.rootViewController;
UIViewController * tempVc =
nil;
while (vc)
{
tempVc = vc;
if ([vc
isKindOfClass:[UINavigationController
class]])
{
vc = [(UINavigationController *)vc
visibleViewController];
}
else
if ([vc isKindOfClass:[UITabBarController
class]])
{
vc = [(UITabBarController *)vc
selectedViewController];
}
else
{
vc = vc.presentedViewController;
}
}
return tempVc;
}
以上方法,我验证了各种我想到的app结构,如果有获取不到的,欢迎留言,我再次补充
- (UIViewController *)appRootViewController
{
UIWindow *keyWindow = [UIApplication
sharedApplication].keyWindow;
UIViewController *vc = keyWindow.rootViewController;
UIViewController * tempVc =
nil;
while (vc)
{
tempVc = vc;
if ([vc
isKindOfClass:[UINavigationController
class]])
{
vc = [(UINavigationController *)vc
visibleViewController];
}
else
if ([vc isKindOfClass:[UITabBarController
class]])
{
vc = [(UITabBarController *)vc
selectedViewController];
}
else
{
vc = vc.presentedViewController;
}
}
return tempVc;
}
以上方法,我验证了各种我想到的app结构,如果有获取不到的,欢迎留言,我再次补充
相关文章推荐
- xcode7 倒腾ios 崩溃日志crash
- ios异常机制
- iOS判断一个日期是否为昨日、今日、今年
- IOS热更新-JSPatch实现原理+Patch现场恢复
- 一步一步教你做ios推送
- 沙盒文件
- iOS此证书的签发者无效
- iOS使用HHealthKit读取修改健康步数
- iOS 应用程序内部国际化,不跟随系统语言
- IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
- IOS开发之WIFI及IP相关
- iOS动画之向右拉的抽屉3D效果
- [iOS]把agent权限指定给开发组的其他成员
- iOS开发之静态库.a的制作教程
- iOS 手机横屏后需要做一些处理
- Visual Studio找不到iOS模拟器
- Visual Studio找不到iOS模拟器
- iOS for MVC模式
- iOS使用HealthKit获取今天步数
- iOS ARC下的内存管理问题以及解决办法