1

In my app I have an ECSlidingViewController declared as initial root controller via Storyboard. In my AppDelegate's didFinishLaunchingWithOptions method, I instantiate it as above:

self.slidingController = [[UIStoryboard storyboardWithName:@"AppStoryboard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ECSlidingViewController"];

What I want is to be able to show a global modal view controller (eg. when a push notification arrives while the app is active) without knowing which controller is currently top in the sliding controller.

What I do is (in my AppDelegate):

[self.slidingController.topViewController presentModalViewController:controller animated:YES];

but it doesn't seem to work.

Is there any way I could present a modal controller from my sliding controller regardless which controller is topViewController?

PS. If no, is there any chance that what I want will work with SWRevealViewController instead of ECSlidingViewController? If it's worth, I will take the painful road to switch.

Thank you in advance!

ozzotto
  • 1,146
  • 3
  • 13
  • 30
  • try `[self.slidingController presentModalViewController:controller animated:YES];` without `.topViewController` – alexcristea Apr 16 '13 at 11:06
  • I 've already tried that. It gives me "Warning: Attempt to present on whose view is not in the window hierarchy!" – ozzotto Apr 16 '13 at 11:19
  • Set the ECSlidingViewController to be the rootViewControllr `self.rootViewController = [[UIStoryboard storyboardWithName:@"AppStoryboard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ECSlidingViewController"];` – alexcristea Apr 16 '13 at 11:21
  • Is there any need to declare this by code since I 've already set the ECSlidingViewController as Initial View Controller in my Storyboard; – ozzotto Apr 16 '13 at 11:28
  • In this case the `self.window.rootViewController` should be `ECSlidingViewController` and you should be able to call `[self.window.rootViewController presentModalViewController:controller animated:YES]`; – alexcristea Apr 16 '13 at 11:43
  • You need to add the view that you're presenting into the window hierarchy. You can do this a couple ways, but I would do: `[self addChildViewController:self.rootViewController];` – jakenberg Apr 16 '13 at 16:34

2 Answers2

1

If the ECSlidingViewController is set as the initial view controller in the storyboard, then why are you instantiating another one in your app delegate code? By doing that, you're calling your methods on a different instance of ECSlidingViewController than the one that's put on screen by the storyboard. This is likely the source of your problem. Instead, get a reference to your ECSlidingViewController like this:

self.slidingController = self.window.rootViewController;

Then try,

self.slidingController.topViewController presentModalViewController:controller animated:YES];

or

self.slidingController presentModalViewController:controller animated:YES];

I haven't worked with ECSlidingViewController, so I don't know which of these might work.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • 1
    That was the issue rdelmar! The instantiation of ECSlidingViewController in AppDelegate. So what I did now is `self.slidingController = (ECSlidingViewController *)self.window.rootViewController;` – ozzotto Apr 17 '13 at 07:27
0

Try this

UIViewController *rootViewController = self.window.rootViewController;
// You now have in rootViewController the view with your "Hello world" label and go button.

// Get the navigation controller of this view controller with:
UINavigationController *navigationController = rootViewController.navigationController;

[navigationController.topViewController presentModalViewController:controller animated:YES];
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • Sorry, this doesn't work. My rootViewController is an ECSlidingViewController whose topViewController isn't always a UINavigationController. Thx! – ozzotto Apr 16 '13 at 11:29