如果没有UINavigationController,我似乎无法得到最顶端的UIViewController。以下是我目前所掌握的:
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(vc, animated: true, completion: nil)
然而,它似乎没有做任何事情。keyWindow和rootViewController似乎也不是nil值,所以可选的链接应该不是问题。
注意:这样做不是一个好主意。它打破了MVC模式。
对于Swift 5+,
iOS 13 +
extension UIViewController {
static func topMostViewController() -> UIViewController? {
if #available(iOS 13.0, *) {
let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
return keyWindow?.rootViewController?.topMostViewController()
}
return UIApplication.shared.keyWindow?.rootViewController?.topMostViewController()
}
func topMostViewController() -> UIViewController? {
if let navigationController = self as? UINavigationController {
return navigationController.topViewController?.topMostViewController()
}
else if let tabBarController = self as? UITabBarController {
if let selectedViewController = tabBarController.selectedViewController {
return selectedViewController.topMostViewController()
}
return tabBarController.topMostViewController()
}
else if let presentedViewController = self.presentedViewController {
return presentedViewController.topMostViewController()
}
else {
return self
}
}
}
用法:
当你在没有UIViewController实例的情况下获取topMostViewController时
guard let viewController = UIViewController.topMostViewController() else { return }
print(viewController)
当你获取UIViewController实例的topMostViewController时
let yourVC = UIViewController()
guard let vc = yourVC.topMostViewController() else { return }
print(vc)