如何在我的应用程序中获取对顶部可见视图控制器的引用。我看到了一些使用 navigationcontroller.[top|visible]viewcontroller 的解决方案。但我不在我的应用程序中使用导航控制器。
这似乎是一个很常见的用例,我发现很难访问 top|visible 视图控制器很奇怪
如何在我的应用程序中获取对顶部可见视图控制器的引用。我看到了一些使用 navigationcontroller.[top|visible]viewcontroller 的解决方案。但我不在我的应用程序中使用导航控制器。
这似乎是一个很常见的用例,我发现很难访问 top|visible 视图控制器很奇怪
这也应该遵循您的模态视图和导航控制器(如果有):
- (UIViewController *)deepestPresentedViewControllerOf:(UIViewController *)viewController
{
if (viewController.presentedViewController) {
return [self deepestPresentedViewControllerOf:viewController.presentedViewController];
} else {
return viewController;
}
}
- (UIViewController *)topViewController
{
UIViewController *rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
UIViewController *deepestPresentedViewController = [self deepestPresentedViewControllerOf:rootViewController];
if ([deepestPresentedViewController isKindOfClass:[UINavigationController class]]) {
return ((UINavigationController *)deepestPresentedViewController).topViewController;
} else {
return deepestPresentedViewController;
}
}
您可能应该在这里使用委托模式(为子视图控制器提供对其可以调用的对象的引用)。如果您编辑帖子以解释为什么您认为需要对顶视图控制器的引用,我们可以为您提供有关如何在您的情况下使用委托模式的建议。
但现在我只会给你绳子,你需要吊死自己:
UIViewController *topVC = [UIApplication sharedApplication].keyWindow.rootViewController;
-(UIViewController *) getTopMostController
{
UIWindow *topWindow = [UIApplication sharedApplication].keyWindow;
if (topWindow.windowLevel != UIWindowLevelNormal)
{
topWindow = [self returnWindowWithWindowLevelNormal];
}
UIViewController *topController = topWindow.rootViewController;
if(topController == nil)
{
topWindow = [UIApplication sharedApplication].delegate.window;
if (topWindow.windowLevel != UIWindowLevelNormal)
{
topWindow = [self returnWindowWithWindowLevelNormal];
}
topController = topWindow.rootViewController;
}
while(topController.presentedViewController)
{
topController = topController.presentedViewController;
}
if([topController isKindOfClass:[UINavigationController class]])
{
UINavigationController *nav = (UINavigationController*)topController;
topController = [nav.viewControllers lastObject];
while(topController.presentedViewController)
{
topController = topController.presentedViewController;
}
}
return topController;
}
-(UIWindow *) returnWindowWithWindowLevelNormal
{
NSArray *windows = [UIApplication sharedApplication].windows;
for(UIWindow *topWindow in windows)
{
if (topWindow.windowLevel == UIWindowLevelNormal)
return topWindow;
}
return [UIApplication sharedApplication].keyWindow;
}
-(UIViewController *)getCurrentViewController
{
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;
else
result = window.rootViewController;
return result;
}