问题
I have a navigationController from where I launch a ModalViewController. In this ModalViewController I will display the MailComposer which itself another ModalViewController.
Now if the user hits the send button the MailComposerView should be dismissed as well the other ModalViewController. For that I call a delegate method in the mailComposerController.
Now only the MailComposerView will be dismissed but no the the other ModalViewController and I get following error message
attempt to dismiss modal view controller whose view does not currently appear. self = <UINavigationController: 0x724d500> modalViewController = <UINavigationController: 0x72701f0>
Do you have any Idea would I'm doing wrong?
First ModalView
- (void)addList {
NSLog(@"addList");
//AddListViewController *addListViewController = [[AddListViewController alloc] init];
AddListViewController *addListViewController = [[AddListViewController alloc] initWithStyle:UITableViewStyleGrouped];
addListViewController.delegate = self;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addListViewController];
navigationController.navigationBar.barStyle = UIBarStyleBlack;
navigationController.navigationBar.translucent = YES;
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[addListViewController release]; }
In the AddListViewController calling the MailView
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
NSString *subject = [NSString stringWithFormat:@"Group invite for groupname: %@", @"mhm"];
[mailComposer setSubject:subject];
// Fill out the email body text
NSString *emailBody = @"This is an group invite bla bla";
[mailComposer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];
In the mailComposerController method
[self.navigationController dismissModalViewControllerAnimated:YES];
[self.delegate finishAddList:checkmark andListName:listName.text];
In the finsihAddList delegate
[self dismissModalViewControllerAnimated:YES];
回答1:
You must call the second dismiss with a delay, because the first dismiss hasn't been done yet when called.
[self performSelector: @selector(finish:) withObject: obj afterDelay: 0.0f];
A delay of 0.0f is intentional, it means it will be done in the next event loop.
回答2:
I had a similar problem. I had a stack of modally presented view controllers. When I tried to dismiss them starting with the visible one, and moving down the stack, I would fail with the same error. The solution was to dismiss the view controller at the bottom of the stack. It would dismiss everything above it.
In your case, my solution would amount to changing the mailComposerController method so that it contains only one line (does not dismiss the top-most modal vie controller).
[self.delegate finishAddList:checkmark andListName:listName.text];
I know you've resolved your problem already, but thought this might be helpful for others.
来源:https://stackoverflow.com/questions/3988432/two-modalviewcontroller