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?
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];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With