3
votes

Why do I want to use presentModalViewController in AppDelegate? - Processing didReceiveLocalNotification, so I can launch a seperate modalView on top of my app to process the notification

What does my view architecture look like? - Using storyboards - MainStoryBoard: ->TabBarController->NavigationController

What's happening? - Nothing, that's the problem :-D - When I press the action button from the UILocalNotification, the app opens, but just shows the last open view from the tabbarcontroller.

As you can see below my last effort was to present the modalViewController on top of that current view, like so: [self.window.rootViewController.tabBarController.selectedViewController.navigationController presentModalViewController:navigationController animated:YES];

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

UIApplicationState state = [application applicationState];
if (state == UIApplicationStateInactive) {
    // Application was in the background when notification was delivered.
    NSLog(@"Received notification while in the background");
}
else {
    NSLog(@"Received notification while running.");
}

MedicationReminderViewController *controller = [[UIStoryboard storyboardWithName:@"ModalStoryBoard" bundle:nil] instantiateViewControllerWithIdentifier:@"MedicationReminderVC"];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
[self.window.rootViewController.tabBarController.selectedViewController.navigationController presentModalViewController:navigationController animated:YES];

}

Update

Seems that this is nil: self.window.rootViewController.tabBarController.selectedViewController.navigationController

Solution

[self.window.rootViewController presentModalViewController:navigationController animated:YES];

2
self.window.rootViewController.tabBarController.selectedViewController.navigationController == nil ?NeverBe
Thank you. Damn yes, just tested and it is in fact nil. No wonder why it didn't work. But why is it nil, and where do I go from here?Pieter
[self.window.rootViewController presentModalViewController:navigationController animated:YES];NeverBe
That does work. However I get a navigationBar on top, but the view (rest of the screen) is black...Pieter
[self.window.rootViewController presentModalViewController:controller animated:YES];NeverBe

2 Answers

6
votes

Try this :

[self.window.rootViewController presentModalViewController:controller
                                                  animated:YES];
0
votes

Have you tried the following?

[self.window.rootViewController.tabBarController.selectedViewController presentModalViewController:navigationController animated:YES];

That said, even if this works, I would really urge you to reconsider your design choices to avoid having to do this. Traversing the navigation stack in this way to access stuff can get very messy and I'd strongly advise against it.