2012-01-25 37 views

Trả lời

10

Lấy đối tượng appdelegate:

MyAppDelegate *tmpDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; 

Như berili gợi ý bạn có thể sử dụng thuộc tính của UINavigationController để truy cập điều khiển xem hiện tại của bạn.

Vì vậy, các mã sẽ trông như thế:

id myCurrentController = tmpDelegate.myNavigationController.topViewController; 

hay:

NSArray *myCurrentViewControllers = tmpDelegate.myNavigationController.viewControllers; 
55

Nếu điều khiển xem gốc của ứng dụng của bạn là một UINavigationController bạn có thể làm điều này:

((UINavigationController*)appDelegate.window.rootViewController).visibleViewController; 

Tương tự như vậy, nếu đó là UITabBarController bạn có thể thực hiện việc này:

((UITabBarController*)appDelegate.window.rootViewController).selectedViewController; 

Tất nhiên, việc truyền rõ ràng như thế này bị bẩn. Tốt hơn là nên tự mình nắm bắt tham chiếu bằng các loại mạnh.

+1

Man Tôi nhìn khắp mọi nơi cho việc này. cảm ơn một tấn. –

4

Đối với bất cứ ai không sử dụng một UINavigationController nhưng thay vì điều khiển giao diện mặc định của họ là một UIViewController bạn có thể kiểm tra xem điều khiển đang hoạt động (hoặc trình bày) những điều sau đây AppDelegate:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int { 
    if let rootViewController = self.window!.rootViewController { 
     if let presentedViewController = rootViewController.presentedViewController { 
      return presentedViewController.supportedInterfaceOrientations() 
     } 
    } // Else current view controller is DefaultViewController 

    return Int(UIInterfaceOrientationMask.Portrait.rawValue) 
} 

Như bạn có thể nhìn thấy Tôi đang kiểm tra bộ điều khiển xem hiện tại để hỗ trợ các định hướng giao diện khác nhau cho các bộ điều khiển xem cụ thể. Đối với bất cứ ai quan tâm đến việc sử dụng phương pháp này để hỗ trợ cụ thể sau đây nên được đặt trong mỗi bộ điều khiển xem cần một định hướng cụ thể.

override func supportedInterfaceOrientations() -> Int { 
    return Int(UIInterfaceOrientationMask.All.rawValue) 
} 

Lưu ý: Mã này đã được viết với Swift 1.2

+0

Thậm chí tốt hơn, thay đổi 'if let' thành' trong khi let' để nó đạt đến đỉnh cao nhất. – milesper

21

này có thể giúp

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

- (UIViewController *)topViewController:(UIViewController *)rootViewController 
{ 
    if (rootViewController.presentedViewController == nil) { 
    return rootViewController; 
    } 

    if ([rootViewController.presentedViewController isKindOfClass:[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]; 
} 

Swift phiên bản:

extension UIApplication { 
    class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? { 
     if let nav = base as? UINavigationController { 
      return topViewController(base: nav.visibleViewController) 
     } 
     if let tab = base as? UITabBarController { 
      if let selected = tab.selectedViewController { 
       return topViewController(base: selected) 
      } 
     } 
     if let presented = base?.presentedViewController { 
      return topViewController(base: presented) 
     } 
     return base 
    } 
} 

Trích từ: https://gist.github.com/snikch/3661188

+1

gotta yêu đệ quy, cảm ơn :) –

8

Bạn có thể nhận được bộ điều khiển xem hiện tại từ rootViewController bằng cách tìm kiếm presentedViewController của nó, như thế này:

UIViewController *parentViewController = [[[UIApplication sharedApplication] delegate] window].rootViewController; 

while (parentViewController.presentedViewController != nil){ 
    parentViewController = parentViewController.presentedViewController; 
} 
UIViewController *currentViewController = parentViewController; 

Nó hoạt động với tôi.Hy vọng nó sẽ giúp :)

+0

Đã lưu ngày của tôi. Cảm ơn +1 – crash

+0

tốt nhất, Cảm ơn –

4

Swift Giải pháp:

self.window.rootViewController.presentedViewController. 

Điều đó sẽ giúp bạn có được những gì bạn cần.

13

Hãy một phần mở rộng:

extension UIApplication { 
    class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? { 
     if let nav = base as? UINavigationController { 
      return topViewController(nav.visibleViewController) 
     } 
     if let tab = base as? UITabBarController { 
      let moreNavigationController = tab.moreNavigationController 

      if let top = moreNavigationController.topViewController where top.view.window != nil { 
       return topViewController(top) 
      } else if let selected = tab.selectedViewController { 
       return topViewController(selected) 
      } 
     } 
     if let presented = base?.presentedViewController { 
      return topViewController(presented) 
     } 
     return base 
    } 
} 

Cách sử dụng:

if let rootViewController = UIApplication.topViewController() { 
    //do sth with root view controller 
} 
+0

Câu trả lời này đã khắc phục sự cố gây phiền toái với các lớp mẫu Nâng cao NSOperations của Apple. Nếu xảy ra lỗi, bộ điều khiển chế độ xem trên cùng là bộ điều khiển duy nhất có thể hiển thị cảnh báo. Tôi có thể đã làm cho nó lặp đi lặp lại thay vì đệ quy, bởi vì tôi là cũ thời, nhưng điều này là tốt hơn nhiều so với mù quáng bằng cách sử dụng UIApplication.sharedApplication(). KeyWindow? .rootViewController –

0

Thường thì tôi cần phải lấy điều khiển xem mà hiện tại đang hiển thị. Nó có thể có nghĩa là bộ điều khiển xem ở trên cùng của ngăn xếp của UINavigationController hiện tại, bộ điều khiển xem hiện được trình bày, vv Vì vậy, tôi đã viết hàm này mà nó ra hầu hết thời gian, và bạn có thể sử dụng bên trong một phần mở rộng UIViewController.

Mã trong Swift 3:

func currentViewController(
_ viewController: UIViewController? = 
    UIApplication.shared.keyWindow?.rootViewController) 
     -> UIViewController? { 
    guard let viewController = 
    viewController else { return nil } 

    if let viewController = 
     viewController as? UINavigationController { 
     if let viewController = 
      viewController.visibleViewController { 
      return currentViewController(viewController) 
     } else { 
      return currentViewController(
       viewController.topViewController) 
     } 
    } else if let viewController = 
      viewController as? UITabBarController { 
     if let viewControllers = 
      viewController.viewControllers, 
      viewControllers.count > 5, 
      viewController.selectedIndex >= 4 { 
      return currentViewController(
       viewController.moreNavigationController) 
     } else { 
      return currentViewController(
       viewController.selectedViewController) 
     } 
    } else if let viewController = 
      viewController.presentedViewController { 
     return viewController 
    } else if viewController.childViewControllers.count > 0 { 
     return viewController.childViewControllers[0] 
    } else { 
     return viewController 
    } 
} 

Gọi nó với: currentViewController()

Các vấn đề liên quan