【问题标题】:MFMailComposeViewController not dismiss with Cancel buttonMFMailComposeViewController 不会通过取消按钮关闭
【发布时间】:2016-12-26 09:44:09
【问题描述】:

我有一个类,它使用一个函数子类化 NSObject,并显示一个 MFMailComposeViewController。这是代码:

MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];

mailController.mailComposeDelegate = self;
[mailController setSubject:@"Sample Subject"];
[mailController setMessageBody:@"Here is some main text in the email!" isHTML:NO];
[mailController setToRecipients:@[self.email]];

UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
UINavigationController *navigationController = tabbarController.selectedViewController;
[navigationController.topViewController presentViewController:mailController animated:YES completion:NULL];

这段代码一切正常。问题是当我想关闭MFMailComposeViewController 时。有时我会崩溃,有时它只是没有发生什么。我已经实现了委托功能:

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    UINavigationController *navigationController = tabbarController.selectedViewController;
    [navigationController.topViewController dismissViewControllerAnimated:YES completion:nil];
}

之后,我尝试从 ViewController 中直接显示和关闭它,一切正常。甚至取消按钮。

我不知道为什么它在我的 ViewController 类中有效,但在我的 NSObject 子类中无效。

当我在日志中看到崩溃时:

-[MFMailComposeInternalViewController _notifyCompositionDidFinish]

【问题讨论】:

  • 你有没有在头文件中添加MFMailComposeViewDelegate...?
  • 是的,我做到了。好吧,我已经在.m文件中添加了它,但它是一样的。
  • 发布一些与崩溃日志相关的信息以获得进一步的帮助。
  • 我已经编辑了帖子。
  • 亲爱的@DavidGoncalves:还没有接受答案吗?您可以投票并接受解决您问题的答案,这将帮助面临同样问题的其他人,这

标签: ios objective-c mfmailcomposeviewcontroller


【解决方案1】:

试试这个,

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 
{
    // Do your work and dismiss after you are done with it.
    [controller dismissViewControllerAnimated:YES completion:nil];
}

希望对您有所帮助。

【讨论】:

  • 感谢您的评论 Naresh,但我忘记指定未调用委托方法。
  • 在展示邮件编写器时分配代理?
【解决方案2】:

在你的单例类中试试这个

    UIViewController *currentViewController;

    - (void)sendEmail:(id) viewController {

 currentViewController=(UIViewController*)viewController;

        NSString * appVersionString =@"";
        NSString *strEmailMessage=@"";
        NSString *strEmailSubject=@"";


        NSArray *toRecipents =@"";



        if ([MFMailComposeViewController canSendMail]) {

            MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
            mc.mailComposeDelegate =viewController;
            [mc setSubject:strEmailSubject];
            [mc setMessageBody:strEmailMessage isHTML:NO];
            [mc setToRecipients:toRecipents];


            [viewController presentViewController:mc animated:YES completion:NULL];


        }
        else{
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error" message:@"Please setup email account" delegate:nil cancelButtonTitle:@"cancle" otherButtonTitles:nil];

            [alert show];
        }

    }

这样设置委托

 mc.mailComposeDelegate =viewController;

关闭 viewController

 - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
    {
        switch (result)
        {
            case MFMailComposeResultCancelled:

                break;
            case MFMailComposeResultSaved:
               break;
            case MFMailComposeResultSent:

                break;
            case MFMailComposeResultFailed:

                break;
        }

        // Close the Mail Interface
        [currentViewController dismissViewControllerAnimated:YES completion:NULL];
    }

我希望这会奏效......

【讨论】:

  • 迪鲁。好的,但我必须将委托方法放在 ViewController 类中。我不能将 NSObject 类设置为委托。太奇怪了。
  • 您可以将此方法写入 AppDelegate.m 类并实现所有委托协议,并将委托设置为“自我”,如果您将 AppDelegate.h 在您的视图控制器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多