【问题标题】:Need help dismissing email composer screen in iOS需要帮助在 iOS 中关闭电子邮件编辑器屏幕
【发布时间】:2013-01-10 17:25:09
【问题描述】:

我有一个应用程序允许用户从他们的 iPhone 发送测试电子邮件。我的应用程序调用一个方法来激活撰写邮件功能,如下所示:

-(void)displayComposerSheet
{

    //set up a way to cancel the email here
    //picker is an instance of MSMailComposeViewController already declared in the .h file

    [picker setSubject:@"Test Mail"];

    // Set up recipients

    // Attach an image to the email
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Icon" ofType:@"png"];
    NSData *myData = [NSData dataWithContentsOfFile:path];
    [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"Icon"];

    // Fill out the email body text
    NSString *emailBody = @"This is a test mail.";
    [picker setMessageBody:emailBody isHTML:NO];

    [self presentModalViewController:picker animated:YES];
    NSLog(@"mail is working");

}


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    emailLabel.hidden = NO;
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MFMailComposeResultCancelled:
            emailLabel.text = @"Mail sending canceled.";
            break;
        case MFMailComposeResultSaved:
            emailLabel.text = @"Mail saved.";
            break;
        case MFMailComposeResultSent:
        {
            emailLabel.text = @"Mail sent.";
            NSLog(@"It's away!");


            UIAlertView *emailAlertView = [[UIAlertView alloc] initWithTitle:@"Sent!" message:@"Mail sent successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [emailAlertView show];
            [self dismissModalViewControllerAnimated:YES];
            [self.navigationController popViewControllerAnimated:YES];


        }
            break;
        case MFMailComposeResultFailed:
        {
            emailLabel.text = @"Mail sending failed.";


        }
            break;
        default:
        {
            emailLabel.text = @"Mail not sent.";

        }
        break;
    }

}

我的问题是当撰写电子邮件功能处于活动状态时,我无法退出此功能并返回我的应用程序。摆脱这种情况的唯一方法是实际继续并发送消息。导航栏的左上角有一个默认的“取消”栏按钮,单击该按钮后,我提供了三个选项:“删除草稿”、“保存草稿”和“取消”。如果我选择“删除草稿”,它只会将我返回到撰写消息屏幕。我有没有办法让用户在启动撰写邮件功能后但在发送之前返回应用程序?有没有办法向“取消”栏按钮添加额外的功能来实现这一点?

提前感谢所有回复的人。

【问题讨论】:

    标签: ios uiviewcontroller mfmailcomposeviewcontroller


    【解决方案1】:

    您必须使用- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result 方法实现MFMessageComposeViewControllerDelegate

    您将在此方法中关闭您的消息视图。

    编辑:我与MFMailComposeViewControllerDelegate 混淆,但目的与MFMessageComposeViewControllerDelegate 相同

    【讨论】:

    • 感谢您的及时回复。我已经编辑了我的问题以包含我已经采用的这种方法。
    • 我不确定你到底想做什么。如果您想在取消时执行一些操作(包括关闭视图),只需在 MFMailComposeResultCancelled case 语句中包含代码即可。
    • 谢谢亚曼。多亏了你的建议,我才弄明白。我添加了行 [self dismissModalViewControllerAnimated:YES];在您提到的方法中,它现在可以按我的意愿工作!
    【解决方案2】:

    ...didFinishWithResult: 方法中查看您自己的代码:

        case MFMailComposeResultCancelled:
            emailLabel.text = @"Mail sending canceled.";
            break;
        case MFMailComposeResultSaved:
            emailLabel.text = @"Mail saved.";
            break;
        case MFMailComposeResultSent:
        {
            emailLabel.text = @"Mail sent.";
            NSLog(@"It's away!");
    
    
            UIAlertView *emailAlertView = [[UIAlertView alloc] initWithTitle:@"Sent!" message:@"Mail sent successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [emailAlertView show];
            [self dismissModalViewControllerAnimated:YES];
            [self.navigationController popViewControllerAnimated:YES];
    
    
        }
    

    当结果为MFMailComposeResultSent 时,您将关闭模态视图控制器并弹出导航堆栈,这会导致邮件撰写视图控制器消失,并且还会弹出堆栈以删除呈现撰写视图的视图控制器控制器。但是,当结果为MFMailComposeResultCancelled 时,您只需设置一些标签的文本。 MFMailComposeResultSaved 也一样。如果您希望撰写视图控制器在用户取消或保存时消失,您也需要在这些情况下关闭消息撰写视图控制器。

    【讨论】:

      【解决方案3】:

      你应该在你的视图控制器中添加这个方法

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

      希望这个对某人有所帮助。

      【讨论】:

        【解决方案4】:

        设置 MFMailComposeViewController 的委托 MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc]init];

        mailcomposer.mailComposeDelegate = self;

        并使用此委托方法 -(void)mailComposeController:(MFMailComposeViewController *)控制器didFinishWithResult:(MFMailComposeResult)结果错误:(NSError *)错误

        【讨论】:

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