UIViewController *result = 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;
}
}
}
UIView *frontView = [[window subviews] objectAtIndex:0];
id nextResponder = [frontView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]){
result = nextResponder;
NSLog(@"%@",result);
}else{
result = window.rootViewController;
NSLog(@"%@",result);
}
本文将介绍如何在iOS应用中获取顶层UIViewController实例,通过遍历窗口和子视图,找到处于正常层级的窗口,进而获取其前端视图并判断是否为UIViewController类型,最后返回对应的UIViewController实例。
448

被折叠的 条评论
为什么被折叠?



