52

MFMailComposeViewControllerUITableViewController. 问题是当我在“邮件撰写”窗口中选择“取消”或“发送”按钮时,永远不会调用委托方法:

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

4 回答 4

219

确保你使用

controller.mailComposeDelegate = self;

并不是

controller.delegate = self;
于 2010-12-25T13:47:59.343 回答
14

您的方法签名不正确:

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

应该:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
于 2009-12-17T06:25:24.683 回答
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
于 2014-03-11T12:27:43.200 回答
2

我遇到了同样的问题,并且正在寻找过去 2 天的修复程序,然后我自己找到了修复程序,你不会相信它有多小。

在我的情况下,视图控制器(根据这个问题说'DetailsTableViewController')MFMailComposeViewController已经从其他视图控制器(比如'BaseViewController')中呈现出来。

问题在于modalPresentationStyle从 BaseViewController 呈现时的“DetailsTableViewController”的“”。

当我将它从 ' UIModalPresentationFormSheet' 更改为 ' UIModalPresentationPageSheet'(除 ' ' 之外的任何东西UIModalPresentationFormSheet)问题得到解决并且邮件控制器委托方法开始照常触发。

注意:我已经在 'DetailsTableViewController' 中调用了下面的方法(对于这个例子),所以我modalPresentationStyle使用哪个 ' ' 并不重要。

    - (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.view.superview.bounds = CGRectMake(0, 0, 1024, 768);
    self.view.superview.backgroundColor = [UIColor clearColor];
}
于 2016-09-29T06:31:23.977 回答