如果UIViewController没有访问权限,我似乎无法获得最高排名UINavigationController。这是我到目前为止的内容:
UIViewController
UINavigationController
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(vc, animated: true, completion: nil)
但是,它似乎没有任何作用。在keyWindow和rootViewController似乎是非零值太多,所以可选的链接不应该是一个问题。
keyWindow
rootViewController
注意: 做这样的事情是一个坏主意。 它破坏了MVC模式。
presentViewController显示了一个视图控制器。它不返回视图控制器。如果您没有使用UINavigationController,则可能正在寻找presentedViewController,您需要从根开始并遍历显示的视图。
presentViewController
presentedViewController
if var topController = UIApplication.sharedApplication().keyWindow?.rootViewController { while let presentedViewController = topController.presentedViewController { topController = presentedViewController } // topController should now be your topmost view controller }
对于Swift 3+:
if var topController = UIApplication.shared.keyWindow?.rootViewController { while let presentedViewController = topController.presentedViewController { topController = presentedViewController } // topController should now be your topmost view controller }
对于iOS 13+
let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first if var topController = keyWindow?.rootViewController { while let presentedViewController = topController.presentedViewController { topController = presentedViewController } // topController should now be your topmost view controller }