How to present a modal view controller outside of a view controller using presentModalViewController:animated?

StackOverflow https://stackoverflow.com/questions/13860872

Question

Let's say you have an application in which you know for sure that there's always a UINavigationController displayed, and that I need to display another view controller modally from outside this controller (for example : because I use the Command pattern and I don't want to give a reference to a view controller to it).

Is there a safe way to get the "root" navigation controller, and call its presentModalViewController:animated method ?

I tried to use [UIApplication sharedApplication].keyWindow.rootViewController but I figured out that it was nil during an alert.

Était-ce utile?

La solution

Are you using a storyboard or separate xibs?

If you're using separate xibs then you will be setting up the UINavigationController in applicationDidFinishLaunching.

You can make the navigation controller a property of the app delegate.

Then you can access the UINavigationController from anywhere by getting the singleton app delegate and getting the navigation controller property from it.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top