2012-01-14 89 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的類。