【问题标题】:MFMailComposeViewController not dismissingMFMailComposeViewController 不关闭
【发布时间】:2011-10-09 21:29:17
【问题描述】:

我有以下在 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];
        }
    }

【问题讨论】:

    标签: iphone objective-c cocoa-touch mfmailcomposeviewcontroller


    【解决方案1】:

    用途:

    dismissViewControllerAnimated:completion:
    

    从 IOS 6.0 中弃用:

    将此方法添加到您的类中:

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

    玩得开心

    【讨论】:

    • 这个方法现在已经被弃用了,希望它可以工作而不是上面的关闭代码...[self dismissViewControllerAnimated:YES completion:nil];
    【解决方案2】:

    可能有几个问题:

    1. 不在 .h 中添加协议实现

      @interface yourClass : UIViewController <MFMailComposeViewControllerDelegate>
      
    2. 不在.m中添加相关函数:

      -(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:    (MFMailComposeResult)result error:(NSError*)error {
           [self dismissModalViewControllerAnimated:YES];
      }
      
    3. 我的错误是没有设置正确的委托,但我修复了它:) 现在它对我有用:

       picker.mailComposeDelegate = self;
      

    【讨论】:

      【解决方案3】:

      “dismissModalViewControllerAnimated:”在 iOS 6.0

      中已弃用

      iOS 7 使用:

      “dismissViewControllerAnimated:completion:”

      【讨论】:

        【解决方案4】:

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

        我不确定 Luda 是否抓住了问题的核心。是否指定委托没有区别,在模态+模态 MFMailComposeViewController 实例的情况下不起作用。

        【讨论】:

          【解决方案5】:

          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)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-08-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多