Pieter
Pieter

Reputation: 1831

Why is my presentModalViewController not working from inside AppDelegate?

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];

Upvotes: 3

Views: 6618

Answers (2)

NeverBe
NeverBe

Reputation: 5038

Try this :

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

Upvotes: 6

Will Pragnell
Will Pragnell

Reputation: 3133

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.

Upvotes: 0

Related Questions