Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application tried to present modally an active controller: uinavigationcontroller

I'm running into an issue where an error is only raised periodically. In fact it seems almost random. Here's what happens, I'm launching a modal view controller with the following code:

- (void)createMessageClicked
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Channel" bundle:nil];
    UINavigationController *nav = [sb instantiateViewControllerWithIdentifier:@"HIComposeMessageNavController"];
    HIComposeMessageViewController *vc = [[nav viewControllers]objectAtIndex:0];

    vc.channel = [self.channels objectAtIndex:0];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}

Most of the time, this works fine. However once in a while the app crashes and raises the error "Application tried to present modally an active controller <UINavigationController>. Any ideas what I'm doing wrong here?

like image 837
Nick ONeill Avatar asked Mar 20 '23 16:03

Nick ONeill


2 Answers

Try instantiating the controller that is embedded in your navigation controller in your storyboard, then create a new instance of a generic navigation controller:

HICompseController *controller = [sb instantiateViewController: 
                                  HIComposeMessageViewController];
UINavigationController *nav = [[UINavigationController alloc]
                               initWithRootViewController:controller];
[self presentViewController:nav animated:YES completion:nil];
like image 95
Steve Avatar answered Apr 20 '23 00:04

Steve


I would suggest setting an ivar for your UINavigationController, because every time the action is triggered you are creating a whole new navigation controller and present it modally.

I suspect it occurs more often when the time in between click actions are close, hence after the modal controller has dismissed but did not get enough time for navigation controller to be deallocated before a new one that is instantiated from the same class is created and presented again modally. By using the same navigation controller, you can at least be sure that it is dismissed before it is presented again via that method.

Try to create an ivar for the navigation controller and reuse that every time in that method.

like image 25
Unheilig Avatar answered Apr 20 '23 00:04

Unheilig