无法关闭MFMailComposeViewController,代理未被调用

53

我正在从一个UITableViewController中调用MFMailComposeViewController。问题是当我在邮件撰写窗口中选择取消发送按钮时,委托方法没有被调用:

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");
}

应用程序没有崩溃。按下取消或发送按钮后,撰写窗口仍然停留在屏幕上,按钮被禁用。我可以按Home键退出应用程序。

我能够从TableView打开其他模态视图,但无法打开MailCompose。

4个回答

219

请确保您使用

controller.mailComposeDelegate = self;

而非

controller.delegate = self;

10
MFMailComposeViewControllerUINavigationController 的子类。他们这样做是为了让你可以实现 UINavigationControllerDelegate 方法。 - Mark Adams
我认为只有一个视图控制器具有不同的委托,也困扰了一段时间。谢谢mxg :) - The iCoder

14

你的方法签名不正确:

- (void)mailComposeController:(MFMailComposeViewController*)controllerdidFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
应该是:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error

我相信这只是他问题中的一个笔误。 - Yuchen

4
请参考这篇文章进行完整的实现: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

这篇文章非常有帮助:http://www.ioscreator.com/tutorials/send-email-from-an-app - Rajeev

3

我也遇到了相同的问题并且已经寻找解决方法两天了,最终自己发现了一个很小的解决方案。

在我的情况下,展示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];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接