0

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.

Community
  • 1
  • 1
David
  • 998
  • 7
  • 17
  • You mean u tried this `[[(AppDelegate *) [[UIApplication sharedApplication] delegate] window] rootViewController];` Right? – hp iOS Coder Dec 13 '12 at 13:46
  • I really meant `[UIApplication sharedApplication].keyWindow.rootViewController` which works pretty well. Is that wrong ? – David Dec 13 '12 at 13:47
  • It should work technically! I dnt know why it is giving u nil – hp iOS Coder Dec 13 '12 at 13:54
  • @hpiOSCoder: I think UIAlertView is in some way "niling" the keyWindow.rootViewController temporarily... There's a simple example you can try in the other question I linked. – David Dec 13 '12 at 14:00
  • In your other question, you said you didn't want to have to dismiss the alert before presenting the modal controller. Why? Alerts are meant to be dealt with right away, and should be dismissed before doing anything else. – rdelmar Dec 13 '12 at 16:47
  • @rdelmar: in my application, the alert is there to display the result of some "asynchronous task", once a task is complete a new task is performed. This next asynchronous task might need (or not) to display a modal view controller to ask some information to the user. I don't want to block this new task if there's not need for user interaction, and I don't want to make a special case either. – David Dec 14 '12 at 08:30

1 Answers1

1

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.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • I'm using neither storyboard nor xibs : all "programmatically". I was expecting a "built-in" way of getting the "root" view controller, but I might use the delegate approach, thanks ! – David Dec 13 '12 at 13:49