2017-03-01 44 views

回答

0

您可以使用以下方法

- (UIViewController *)topViewController{ 
    return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; 
} 
- (UIViewController *)topViewController:(UIViewController *)rootViewController{ 
    if(rootViewController.presentedViewController == nil){ 
     return rootViewController; 
    } 
    if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) { 
     UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController; 
     UIViewController *lastViewController = [[navigationController viewControllers] lastObject]; 
     return [self topViewController:lastViewController]; 
    } 
    UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController; 
    return [self topViewController:presentedViewController]; 
} 

找到頂視圖控制器,你可以用這個方法,我已經做了,但是,創建一個類我的Mac,我在我的手機上輸入這個對不起,格式不好。

在創建從任何地方

[[UIApplication sharedApplication].keyWindow.rootViewController] topViewController]; 

類別打電話時我回到我的Mac

編輯

繼承人的類別,我將添加此類別:

@interface UIViewController (TopViewController) 
- (UIViewController *)topViewController; 
@end 



#import "UIViewController+TopViewController.h" 

@implementation UIViewController (TopViewController) 

- (UIViewController *)topViewController { 
    return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; 
} 

- (UIViewController *)topViewController:(UIViewController *)rootViewController 
{ 
    if (rootViewController.presentedViewController == nil) { 
     return rootViewController; 
    } 
    if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) { 
     UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController; 
     UIViewController *lastViewController = [[navigationController viewControllers] lastObject]; 
     return [self topViewController:lastViewController]; 
    } 
    UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController; 
    return [self topViewController:presentedViewController]; 
} 

@end 

忘了提及我有這個從here

+0

如果你創建類,並添加斯威夫特再也沒有問題。 –

+0

不管是什麼頂級視圖控制器,它會返回tabbar,如果它是 –

+0

如果呈現的話不會返回alertcontroller嗎? –

3

方法這裏的UIApplication的迅速擴展查找頂視圖控制器

extension UIApplication 
{ 
    class func topViewController(_ base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? 
    { 
     if let nav = base as? UINavigationController 
     { 
      let top = topViewController(nav.visibleViewController) 
      return top 
     } 

     if let tab = base as? UITabBarController 
     { 
      if let selected = tab.selectedViewController 
      { 
       let top = topViewController(selected) 
       return top 
      } 
     } 

     if let presented = base?.presentedViewController 
     { 
      let top = topViewController(presented) 
      return top 
     } 
     return base 
    } 
} 

調用與

let topController = UIApplication.topViewController() 
相關問題