Get top most UIViewController

I can’t seem to get the top most UIViewController without access to a UINavigationController. Here is what I have so far:

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(vc, animated: true, completion: nil)

However, it does not seem to do anything. The keyWindow and rootViewController seem to be non-nil values too, so the optional chaining shouldn’t be an issue.

NOTE: It is a bad idea to do something like this. It breaks the MVC pattern.

27 Answers
27

Leave a Comment