如何在呈现UIImagePickerController时获取顶视图控制器?

时间:2015-12-19 08:56:38

标签: ios swift uistoryboard viewcontroller

有时我会提出UIImagePickerViewController。呈现后,我调用我的函数:UIStoryboard.topViewController()

extension UIStoryboard {

    class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {

        if let nav = base as? UINavigationController {
            return topViewController(nav.visibleViewController)
        }

        if let svc = base as? UISplitViewController where svc.viewControllers.count == 1 {
            return topViewController(svc.viewControllers[0])
        }

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

        return base
    }
}

当我打印结果时,我得到的是:

  

0x000000014cb2aa00
   {
    UIKit.UIResponder = {...}
  }

如何从UIImagePickerController功能获取topViewController()

1 个答案:

答案 0 :(得分:0)

对您的案例不是一个完整的答案,但这里是我如何找到我的程序中的顶级VC。您应该可以根据自己的情况编辑它。

class UIHelper {
static func getCurrentViewController() -> UIViewController? {
    var currentViewController: UIViewController?
    if let window = UIApplication.sharedApplication().delegate?.window {
        currentViewController = window!.rootViewController?.presentedViewController
    }
    if currentViewController == nil {
        return nil
    }

//Check for my version of my main tab bar VC
    if let tabBarController = currentViewController as? RootTabBarController {
        currentViewController = tabBarController.selectedViewController
        print("Tab bar presents \(currentViewController)")
    }
// Check if it's a nav VC
    if let navController = currentViewController as? UINavigationController {
        currentViewController = navController.viewControllers[0] as? UIViewController
        print("Nav controller presents \(currentViewController)")
    }

    print("Current controller: \(currentViewController)")

    return currentViewController
}
}