2012-01-14 94 views
2

我使用MFMailComposeViewController在我的应用程序中发送邮件。但是,当目前的邮件撰写视图控制器时,所有的导航按钮都被禁用(除了选择邮件地址屏幕中的后退按钮),我必须使用主页按钮退出应用程序。有没有人有想法? 下面是截图: Screen shot 2MFMailComposeViewController禁用导航栏按钮


代码:

 
- (void)shareVieEmail 
{ 
    if ([MFMailComposeViewController canSendMail]) { 
     MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init]; 
     mailViewController.mailComposeDelegate = self; 
     [mailViewController setSubject:@"Test subject"]; 
     [mailViewController setMessageBody:@"Mail message body" isHTML:NO]; 

     NSData *imageData = [NSData dataWithContentsOfFile:photourl]; 
     [mailViewController addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"example_photo"]; 
     [self presentModalViewController:mailViewController animated:YES]; 
    } else { 
     [[[UIAlertView alloc] initWithTitle:@"Cannot send mail" message:@"Device is unable to send email in its current state" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]; 
    } 
} 

委托方法:

 
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 
{ 
    switch (result) 
    { 
     case MFMailComposeResultCancelled: 
      //NSLog(@"Result: canceled"); 
      break; 
     case MFMailComposeResultSaved: 
      //NSLog(@"Result: saved"); 
      break; 
     case MFMailComposeResultSent: 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result" message:@"Mail Sent Successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
      [alert show]; 
     } 
      break; 
     case MFMailComposeResultFailed: 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result" message:@"Mail Sent Failed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
      [alert show]; 
     } 
      break; 
     default: 
      //NSLog(@"Result: not sent"); 
      break; 
    } 
    if (error) { 
     [[[UIAlertView alloc] initWithTitle:@"Cannot send mail" message:[NSString stringWithFormat:@"ERROR:%@", [error userInfo]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]; 
    } 
    [self dismissModalViewControllerAnimated:YES]; 
} 

而且在头文件中,我宣布实施MFMailCompos eViewControllerDelegate。

+0

可以显示用于显示控制器的代码吗? – mvds 2012-01-14 07:08:57

+0

@mvds我发布了我的代码。 – youshunei 2012-01-14 16:34:51

+0

奇怪,看起来不错。它可能与你的电子邮件设置有关吗?它在所有设备上都是这样吗? – mvds 2012-01-15 13:08:14

回答

2

我有完全相同的问题。我花了一段时间来算出来,但毫无疑问它来到了定制的UIBarButtonItem

我敢打赌,你的UIBarButtonItem.h有一个方法

-(void)setEnabled:(BOOL)enabled ; 

和实施看起来是这样的:

-(void)setEnabled:(BOOL)enabled { 
    if (self.customView) { 
     if ([[self.customView.subviews objectAtIndex:0] isKindOfClass:[UIButton class]])   { 
      ((UIButton*)[self.customView.subviews objectAtIndex:0]).enabled = enabled; 
     } 
    } 
} 

这是造成问题,所以一旦你注释掉这种方法你的问题应该消失。

+0

是啊! ^^。你对!!,谢谢!我会重写这个方法。此问题已解决。 – youshunei 2012-01-31 15:34:47

-1

在你的MFMailComposeViewController的委托中,你需要实现didFinishWithResult:并从那里关闭模态视图控制器。

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 
    // you can test the result of the mail sending here if you want 

    [self dismissModalViewControllerAnimated:YES]; 
} 
+0

我实现了didFinishWithResult委托方法。 – youshunei 2012-01-14 16:33:42

0

我也有这个问题,但我的情况,那是因为我已经从UINavigationController作为CNContactViewController在此workaround提出了一个bug覆盖setNavigationBarHidden:animated:。一个解决方案仍然包括解决方法并解决MFMailComposeViewController中的问题,方法是使用方法调整来调用原始方法或重写的方法,具体取决于当前的topViewController的类。