janeway
janeway

Reputation: 33

dismissModalViewController then popViewController

I have been searching for a solution to my problem but haven't found anything so far.

I have a UINavigationController with a stack of UIViewControllers (this is all within a TabbarController, if that is relevant). At the last ViewController, I want to send an email:

MFMailComposeViewController *emailVC = [[MFMailComposeViewController alloc] init];
// fill out emailVC properties ...
[self presentModalViewController:emailVC animated:YES];

Then in the delegate after the email has been sent, I want to close the email viewcontroller and also pop off the last viewcontroller in the NavigationController stack:

-(void)mailComposeController:(MFMailComposeViewController *)controller
      didFinishWithResult:(MFMailComposeResult)result
                    error:(NSError *)error
    {
        // save some variables here ...
        [self dismissModalViewControllerAnimated:YES]; // This line works by itself
        [self.navigationController popViewControllerAnimated:NO]; // this line causes EXC_BAD_ACCESS

However, the last line causes a crash somewhere. I have checked the ViewController stack before and after. The last viewController correctly gets removed from the list.

Any thoughts or suggestions are welcome. The problem could lie somewhere else in my code I want to make sure I have this part ok. Thank you.

Upvotes: 2

Views: 3222

Answers (1)

Till
Till

Reputation: 27587

Try initiating the pop delayed

iOS 3 and later solution

-(void)mailComposeController:(MFMailComposeViewController *)controller
      didFinishWithResult:(MFMailComposeResult)result
                    error:(NSError *)error
{
    [...]
    [self dismissModalViewControllerAnimated:YES];
    [self performSelector:@selector(doThePop) withObject:nil afterDelay:0.40];
    [...]
}

- (void)doThePop
{
    [self.navigationController popViewControllerAnimated:NO];
}

You may want to fine-tune the delay.

iOS 5 and later solution

-(void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError *)error
{
    [...]
    [self dismissViewControllerAnimated:YES completion:^
    {
        [self.navigationController popViewControllerAnimated:NO];
    }];
    [...]
}

Even though this seems a bit hackish, it should work.

Upvotes: 5

Related Questions