简体   繁体   中英

MFMailComposeViewController breaks navigationController's behavior

Ok here is the situation: I have a main ViewController(mainVC) with a Navigation Controller(NC) and two child ViewControllers(childVC1-childVC2).

The navigation bar of NC gets hidden when viewWillAppear gets called on mainVC:
- (void) viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:TRUE animated:animated]; }

The navigation bar is shown before any child view is pushed on the mainVC via NC:
mainVC *childVC1 = [[childVController1 alloc] initWithNibName:@"childVController1" bundle:nil];
[self.navigationController setNavigationBarHidden:FALSE animated:TRUE];
[self.navigationController pushViewController:childVC1 animated:YES];
[childVC1 release];

The navigation bar is hidden again via viewWillAppear of mainVC, when it gets called after any child view is popped via the standard Back button on the navigation bar.

All works smoothly until a MFMailComposeViewController, the standard mail viewController is called via presentModalViewController method from any child viewController:
- (void) sendMail {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:[NSString stringWithFormat:@"Subject"];
NSString emailBody=[NSString stringWithFormat:@"Mail Message Body"]];
[picker setMessageBody:emailBody isHTML:NO];
[picker setToRecipients:[NSArray arrayWithObject:@"john.appleseed@apple.com"]];
if (picker != nil) {
[self presentModalViewController:picker animated:YES];
[picker release];
} else {
NSLog(@"No email configured");
}
}
- (void)mailComposeController:(MFMailComposeViewController )controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissModalViewControllerAnimated:YES];
}

Now the mail viewController is dismissed (sent mail), and we are on the child viewController.

At this point if we tap the Back button on the navigation bar in order to return to the mainVC, the navigation bar does not get hidden. Because viewWillAppear method on mainVC does not get called.

I have tried to implement a work around by introducing a delegate for the navigation controller from within the child view, thereby hiding the NavBar and popping the child view after the dismmissal of mail view controller (sent mail).

It seemed to do the trick, but following the above actions (sent mail, hide NavBar and pop child view) if I immediately push any child view (emailer childVC1 or the other rather flat childVC2) and then pop it back via the Back button, the navigation bar does not get hidden again! Because viewWillAppear method on mainVC isn't still getting called. viewWillDisappear on childVCs aren't called either.

I conclude that by calling the mail viewController via presentModalViewController method from any child viewController, I somehow break the navigation mechanism of the navigationController.

Note that the mainVC is shown inside a TabBarItem. And if I go to another tab and return back to this one, the behavior of navigationController is restored - that is until I sent another mail.

There must be something I am not doing properly. Any ideas?

您是否尝试将隐藏代码放入viewDidAppear而不是viewWillAppear中?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM