无法解雇MFMailComposeViewController,委托未调用

时间:2009-12-16 23:58:31

标签: ios objective-c cocoa-touch mfmailcomposeviewcontroller

我从MFMailComposeViewController致电UITableViewController。 问题是当我在邮件撰写窗口中选择取消发送按钮时,从未调用委托方法:

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 个答案:

答案 0 :(得分:212)

确保使用

controller.mailComposeDelegate = self;

而不是

controller.delegate = self;

答案 1 :(得分:14)

您的方法签名不正确:

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

应该是:

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

答案 2 :(得分: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

答案 3 :(得分:0)

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

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

问题在于modalPresentationStyle&#39; &#39; DetailsTableViewController&#39;从BaseViewController中呈现它。

我从&#39; UIModalPresentationFormSheet&#39;更改了它的那一刻到&#39; UIModalPresentationPageSheet&#39; (就此而言,除了&#39; UIModalPresentationFormSheet&#39;)之外的任何问题都得到了解决,邮件控制器委托方法也照常启动。

注意:我已经在&#39; DetailsTableViewController&#39;中调用了以下方法。 (对于这个例子)所以对我来说并不重要的是modalPresentationStyle&#39;我正在使用。

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