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

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

문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top