2012-11-14 56 views
1

如何在我的应用中获取对顶部可见视图控制器的引用。我看到一些使用navigationcontroller的解决方案。[top | visible] viewcontroller。但我不在我的应用程序中使用导航控制器。在iOS中获取对顶部视图控制器的引用

这似乎是一个很常见的情况,我觉得很奇怪,很难获得进入顶部|可见视图控制器

+0

所以你在你的情况下使用什么? – iDev

回答

1

或许你也应该在这里使用委托模式(给孩子视图控制器引用它可以调用的对象)。如果你编辑你的帖子来解释为什么你认为你需要参考顶视图控制器,我们可以给你建议如何在你的情况下使用委托模式。

但现在我只是给你的绳子,你需要挂自己:

UIViewController *topVC = [UIApplication sharedApplication].keyWindow.rootViewController; 
3

这也应该遵守模态的意见和导航控制器(如果有的话):

- (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; 
    } 
} 
+0

很好的答案,非常感谢。 –

0
-(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; 
} 
0
-(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; 
} 
+0

你又回答同样的问题了吗?如果您认为自己找到了更好的解决方案,那么可以考虑删除旧帖子。谢谢。 – Pang

相关问题