【问题标题】:MFMailComposeViewController's Cancel Button (action sheet possibly) freezes the viewMFMailComposeViewController 的取消按钮(可能是操作表)冻结视图
【发布时间】:2012-12-13 18:52:27
【问题描述】:

我之前看到过几个问题,例如this,但由于缺乏公认的答案以及根据需要实施了一切,我仍然继续面临以下问题: 我显示邮件编写器,但单击取消时,编写器视图冻结。我认为这是由于保存/删除草稿操作表出现在可见框架之外。是的,我已将 mailComposeDelegate 设置为呈现视图控制器,并阅读了几个类似的问题,其中用户尚未处理 (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 委托以在取消时关闭作曲家。我也处理过这个问题,但我似乎一辈子都无法弄清楚为什么在我的通用应用程序的 iPhone 版本中,操作表不会出现在屏幕的可见区域中。以 NSLogged 模态显示邮件编写器的视图控制器的视图框架是 (0,0,320,480)。我的应用程序是通用的,邮件编辑器在 iPad 上完美运行。下面是在 iPhone Simulator 5.1 上运行的作曲家视图的屏幕截图:-


这是显示作曲家的代码:

-(IBAction)mailButtonPressed:(id)sender {

    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    [controller setSubject:@"Subject"];
    [controller setMessageBody:@"Test" isHTML:YES];
    [controller setToRecipients:nil];

    if(controller) {
        [self presentModalViewController:controller animated:YES];
        [controller release];
    }
}

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

【问题讨论】:

  • 你能发布你如何呈现邮件编辑器的代码吗?
  • 我已经用添加的代码编辑了问题
  • 请同时发布 didFinishWithResult 委托方法的代码
  • 触发问题,然后在调试器中暂停。运行此调试器命令:po [[UIApp keyWindow] recursiveDescription]。复制输出并将其粘贴到您的问题中。
  • 我有一个技巧,而不是 [self presentModalViewController:controller animated:YES];,使用 appDelegate 的 rootviewcontroller 类,通过在当前类中创建 AppDelegate [[UIApplication sharedApplication] 委托] 的实例然后用 appDelegate 替换 self 。(你的 rootviewController 类)。如果这能解决您的问题,请告诉我。

标签: iphone ios mfmailcomposeviewcontroller mfmailcomposer cancel-button


【解决方案1】:

为什么不尝试删除代码,然后按照这样的在线教程重试:

http://iphonedevsdk.com/forum/tutorial-discussion/43633-quick-tutorial-on-how-to-add-mfmailcomposeviewcontroller.html

在这种情况下,您总是会忘记工作所需的一行简单代码,因此遵循教程可以帮助我确保所有必要的代码都在那里。

试试这个代码:

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }

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

【讨论】:

  • 我有 :) 这个问题很烦人,我一直用显微镜观察,以免错过一些琐碎的事情 :)
  • 使用“控制器”而不是“自我”。否则将无法正常工作。
【解决方案2】:

在此处使用整个代码作为消息:

.h

    #import <MessageUI/MFMailComposeViewController.h>
    @interface EmailViewController : UIViewController<MFMailComposeViewControllerDelegate>

.m

  -(IBAction)Email {

        MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
            [composer setMailComposeDelegate:self];
            if ([MFMailComposeViewController canSendMail]) {
                [composer setToRecipients:[NSArray arrayWithObjects:@"contact_aaron@easy.com", nil]];
                [composer setSubject:@"Idea for Basic Calculator"];
                [composer setMessageBody:@"My idea is:" isHTML:NO];
                [composer setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
                [self presentModalViewController:composer animated:YES];

    }

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    if (error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error with message" message:[NSString stringWithFormat:@"Error %@", [error description]] delegate:nil cancelButtonTitle:@"Try Again Later!" otherButtonTitles:nil, nil];
        [alert show];
        [self dismissModalViewControllerAnimated:YES];
    }
    else {
        [self dismissModalViewControllerAnimated:YES];
    }
}

【讨论】:

  • 谢谢,但问题本质上不是代码,因为正如我在问题的 cmets 字符串中提到的那样,问题中发布的代码在其他视图中完美运行。
【解决方案3】:

您尝试使用的设备可能由于某种原因无法发送邮件

你可以通过[MFMailComposeViewController canSendMail]查看这个

【讨论】:

    【解决方案4】:

    中应用这个
    -(void)displayComposerSheet
    
    if(composer != nil) {
    
        [composer release];
    
        composer = nil;
    
    }
    

    赋予这个对象价值后

    [picker composer];
    

    【讨论】:

      【解决方案5】:

      您似乎没有使用自动引用计数 (ARC)。您过早地发布了邮件撰写控制器。解散后释放它。

      去掉这一行:[controller release]

      if(controller) {
          [self presentModalViewController:controller animated:YES];
          /// remove this: ---->  [controller release];
      }
      

      并在此处释放此控制器

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

      【讨论】:

        【解决方案6】:

        将此添加到您的(所有)UIViewControllers 以检查您的 iPhone 是否耗尽或内存。这可以解释奇怪的行为,即 mailComposerSheet 释放了你的基本 viewController,所以 mailComposerDelegate 是 nil。

        - (void)didReceiveMemoryWarning
        {
          NSLog(@"didReceiveMemoryWarning");
          [super didReceiveMemoryWarning];
        }
        

        【讨论】:

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