无法closuresMFMailComposeViewController,委托没有被调用
我从UITableViewController
调用MFMailComposeViewController
。 问题是当我在邮件撰写窗口中select“ 取消”或“ 发送”button时,从不会调用委托方法:
mailComposeController:(MFMailComposeViewController*)controllerdidFinishWithResult
这是表视图类:
@implementation DetailsTableViewController - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section==0 && indexPath.row==4) { //SEND MAIL MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init]; controller.mailComposeDelegate = self; if ([MFMailComposeViewController canSendMail]) { [controller setSubject:[NSString stringWithFormat:@"Ref %@",[item objectForKey:@"reference"]]]; [controller setMessageBody:@" " isHTML:NO]; [controller setToRecipients:[NSArray arrayWithObject:[item objectForKey:@"email"]]]; [self presentModalViewController:controller animated:YES]; } [controller release]; } } - (void)mailComposeController:(MFMailComposeViewController*)controllerdidFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { // NEVER REACHES THIS PLACE [self dismissModalViewControllerAnimated:YES]; NSLog (@"mail finished"); }
该应用程序不会崩溃。 按下“取消”或“发送”button后,“撰写窗口”将保留在禁用button的屏幕上。 我可以退出应用程序按Home键。
我能够打开其他模态视graphics式TableView而不是MailCompose。
确保你使用
controller.mailComposeDelegate = self;
并不是
controller.delegate = self;
您的方法签名不正确:
- (void)mailComposeController:(MFMailComposeViewController*)controllerdidFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
应该:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
请参阅这篇文章的完整实现: http : //www.ioscreator.com/tutorials/send-email-from-an-app
取消弃用之后的工作代码:
#import <MessageUI/MFMailComposeViewController.h> @interface SettingsTableViewController () <MFMailComposeViewControllerDelegate, UITextFieldDelegate, UITextViewDelegate> @end @implementation SettingsTableViewController // add default methods -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger sectionNum = indexPath.section; NSInteger rowNum = indexPath.row; if (sectionNum == 2 && rowNum == 1) { MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init]; controller.mailComposeDelegate = self; if ([MFMailComposeViewController canSendMail]) { [controller setSubject:[NSString stringWithFormat:@"Invitation to Northstar app"]]; [controller setMessageBody:@" " isHTML:NO]; // [controller setToRecipients:[NSArray arrayWithObject:[item objectForKey:@"email"]]]; //presentViewController:animated:completion: [self presentViewController:controller animated:YES completion:NULL]; } } } - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { NSLog (@"mail finished"); [self dismissViewControllerAnimated:YES completion:NULL]; } @end
我面临同样的问题,并从过去的两天寻找修复,然后我find了自己的修复,你不会相信它是多么的微小。
在我的情况下,从我介绍MFMailComposeViewController
的视图控制器(说'DetailsTableViewController'根据这个问题)已经从其他视图控制器(如'BaseViewController')呈现。
当从BaseViewController中显示时,问题就出现在“DetailsTableViewController”的“ modalPresentationStyle
”中。
当我从“ UIModalPresentationFormSheet
”更改为“ UIModalPresentationPageSheet
”(就此而言,除了' UIModalPresentationFormSheet
'之外的任何其他事情)问题得到解决,邮件控制器委托方法像往常一样开始启动。
注意:我已经在“DetailsTableViewController”中调用了下面的方法(对于这个例子),所以对于我使用的' modalPresentationStyle
'来说并不重要。
- (void)viewWillLayoutSubviews{ [super viewWillLayoutSubviews]; self.view.superview.bounds = CGRectMake(0, 0, 1024, 768); self.view.superview.backgroundColor = [UIColor clearColor]; }