MFMailComposeViewController无法关闭

8

我有以下代码,在didSelectRowAtIndexPath中调用。问题是,当我点击取消按钮时,会提示保存草稿或放弃。但是当我点击其中任何一个后,视图并没有消失。我在之前iOS 5的应用程序中使用了相同的代码,而且视图可以正常消失。有什么想法吗?在接口中我有MFMailComposeViewController代理协议。

    if (indexPath.row == 0)
    {
        if([MFMailComposeViewController canSendMail])
        {

            MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
            picker.mailComposeDelegate = self;

            [picker setSubject:@"Support"];

            NSArray *toRecipients = [NSArray arrayWithObject:@"contact@app.com"]; 

            [picker setToRecipients:toRecipients];

            NSString *emailBody = text;
            [picker setMessageBody:emailBody isHTML:NO];

            [self presentModalViewController:picker animated:YES];
        }
    }
5个回答

18

使用:

dismissViewControllerAnimated:completion:

iOS 6.0后已过时:

在您的类中添加此方法:

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    [self dismissModalViewControllerAnimated:YES];
}

玩得开心


这个方法现在已经被弃用了,希望这个可以代替上面的代码...[self dismissViewControllerAnimated:YES completion:nil]; - mavericks

7

可能会有几个问题:

  1. Not adding protocol implemantation in the .h

    @interface yourClass : UIViewController <MFMailComposeViewControllerDelegate>
    
  2. Not adding the relevant function in .m:

    -(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:    (MFMailComposeResult)result error:(NSError*)error {
         [self dismissModalViewControllerAnimated:YES];
    }
    
  3. My error was not setting the correct delegate, but I fixed it :) and now it works for me:

     picker.mailComposeDelegate = self;
    

1

"dismissModalViewControllerAnimated:"在iOS 6.0中已过时。

iOS 7使用:

"dismissViewControllerAnimated:completion:"


0

我在这里更详细地描述了问题以及解决方法https://stackoverflow.com/a/13576408/691660

我不确定Luda是否抓住了问题的核心。无论您是否指定委托,对于模态+模态MFMailComposeViewController实例都不起作用。


0

Swift 实现:

确保每次执行其函数时,MFMailComposeViewController 协议和代理都被调用。

这解决了 MFMailComposeViewController 未被解除显示的问题。

     let subj = "Test"
     let messageBody = "Test"
     let toRecipents = ["example@xyz.com"]
     let mc: MFMailComposeViewController = MFMailComposeViewController()
     mc.mailComposeDelegate = self
     mc.setSubject(subj)
     mc.setMessageBody(messageBody, isHTML: true)
     mc.setToRecipients(toRecipents)
     self.present(mc, animated: true, completion: nil)

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