iPhone: How to Close MFMailComposeViewController?

安稳与你 提交于 2019-12-22 09:13:07

问题


I'm having difficulties closing an email message that I have raised.

The email opens nicely, but once it is opened it will not close as the mailComposeController:mailer didFinishWithResult:result error:error handler never gets invoked.

As far as I can tell I have all the bits in place to be able to do this.

Anyone any ideas of what I can look at?

Here is how I raise the email:

-(IBAction)emailButtonPressed 
{

NSString *text = @"My Email Text";

 MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
 mailer.delegate = self;

 [mailer setSubject:@"Note"];
 [mailer setMessageBody:text isHTML:NO];
 [self presentModalViewController:mailer animated:YES];
 [mailer release];
}

and later in the class I have this code to handle the close (but it never gets called):

-(void)mailComposeController:(MFMailComposeViewController *)mailer didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
 [self becomeFirstResponder];
 [mailer dismissModalViewControllerAnimated:YES];
}

My header file is defined as:

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@interface myViewController : UIViewController <UIActionSheetDelegate, UIAlertViewDelegate, MFMailComposeViewControllerDelegate, UINavigationControllerDelegate>

Thanks

Iphaaw


回答1:


You are setting the delegate wrong, the delegate property in MFMailComposeViewController is called mailComposeDelegate, so it should be:

mailer.mailComposeDelegate = self;

Another possible error I can see is calling dismissModalViewControllerAnimated: on mailer - you should send this message to the view controller who presented the mail interface - self in this case:

[self dismissModalViewControllerAnimated:YES];

I wrote "possible error" because it might actually work if iOS routes the message through responder chain, anyway - the documentation says it should be send to presenter.



来源:https://stackoverflow.com/questions/4104199/iphone-how-to-close-mfmailcomposeviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!