Ios 无法解除MFMailComposeViewController,未调用委托

Ios 无法解除MFMailComposeViewController,未调用委托,ios,objective-c,cocoa-touch,mfmailcomposeviewcontroller,Ios,Objective C,Cocoa Touch,Mfmailcomposeviewcontroller,我正在从UITableViewController调用MFMailComposeViewController。 问题是,在“邮件撰写”窗口中选择“取消”或“发送”按钮时,从未调用委托方法: mailComposeController:(MFMailComposeViewController*)controllerdidFinishWithResult 以下是表视图类: @implementation DetailsTableViewController - (void)tableView:(

我正在从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");
}
应用程序没有崩溃。按下Cancel(取消)或Send(发送)按钮后,Compose(撰写)窗口将保持在屏幕上,且按钮处于禁用状态。我可以按Home键退出应用程序


我可以从TableView打开其他模式视图,但不能打开MailCompose。

您的方法签名不正确:

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

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
确保你使用

controller.mailComposeDelegate = self;
而不是

controller.delegate = self;

请参阅本文以了解全面实施:

删除弃用代码后的工作代码:

#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

我也遇到了同样的问题,在过去的两天里我一直在寻找解决方案,然后我自己找到了一个解决方案,你不会相信它有多小

在我的例子中,视图控制器说“DetailsTableViewController”,根据这个问题,在我演示MFMailComposeViewController的地方,已经有其他视图控制器说“BaseViewController”

问题在于“DetailsTableViewController”的“modalPresentationStyle”在BaseViewController中呈现时存在

从我将其从“UIModalPresentationFormSheet”更改为“UIModalPresentationPageSheet”的那一刻起,除“UIModalPresentationFormSheet”之外的任何问题都得到了解决,邮件控制器委托方法开始照常启动

注意:对于这个示例,我已经在“DetailsTableViewController”中调用了下面的方法,所以使用哪个“modalPresentationStyle”对我来说并不重要

    - (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.view.superview.bounds = CGRectMake(0, 0, 1024, 768);
    self.view.superview.backgroundColor = [UIColor clearColor];
}

MFMailComposeViewController是UINavigationController的一个子类。他们这样做是为了让你可以实现UINavigationControllerDelegate方法。我认为这是唯一一个视图控制器有不同的委托,也困扰了一段时间。谢谢mxg:这篇文章非常有用:我相信这只是他的问题中的一个输入错误。