2014-04-11 43 views
3

我有一個應用程序,我使用電子郵件發展商按鈕。當我按下按鈕VEMailView-控制器打開。 這只是MFMailComposeViewController的包裝。MFMailComposeViewController不關閉

當我按下「發送」按鈕,控制器必須關閉,但我看到只是白色的窗口。不再。它必須關閉到主ViewController。怎麼修?

這是我的代碼:

#import <MessageUI/MessageUI.h> 

#import "VEMailView.h" 

@interface VEMailView() < 
    MFMailComposeViewControllerDelegate, 
    UINavigationControllerDelegate 
> 
// UILabel for displaying the result of the sending the message. 

@end 


@implementation VEMailView{ 
    BOOL alreadyOpened; 
} 



- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    alreadyOpened = NO; 

} 
- (void) viewDidAppear:(BOOL)animated { 
[self showMailPicker]; 

} 

- (void)showMailPicker 
{ 

    if ([MFMailComposeViewController canSendMail]) 

    { 
     [self displayMailComposerSheet]; 
    } 

} 

- (void)displayMailComposerSheet 
{ 
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
    picker.mailComposeDelegate = self; 

    [picker setSubject:@"iOS"]; 

    NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"]; 

    [picker setToRecipients:toRecipients]; 

    NSString *emailBody = @" "; 
    [picker setMessageBody:emailBody isHTML:NO]; 

    [self presentViewController:picker animated:YES completion:NULL]; 
} 

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

    switch (result) 
    { 
     case MFMailComposeResultCancelled: 
      NSLog(@"Result: Mail sending canceled"); 
      break; 
     case MFMailComposeResultSaved: 
      NSLog(@"Result: Mail saved"); 
      break; 
     case MFMailComposeResultSent: 
      NSLog(@"Result: Mail sent"); 
      break; 
     case MFMailComposeResultFailed: 
      NSLog(@"Result: Mail sending failed"); 
      break; 
     default: 
      NSLog(@"Result: Mail not sent"); 
      break; 
    } 
[self dismissViewControllerAnimated:YES completion:nil]; 
} 



@end 
+0

'[self dismissViewControllerAnimated:YES completion:nil];'=>'[controller dismissViewControllerAnimated:YES completion:nil];' – Larme

+0

@Larme,no。它不起作用。 – Vyacheslav

+0

我在這裏看不到任何問題。你能發送你的示例代碼嗎?我試過你的代碼,但我什麼都看不到 – VietHung

回答

1

我找到了我自己的解決方案。

爲了解決這個問題,改變

[self dismissViewControllerAnimated:YES completion:nil]; 

[self dismissViewControllerAnimated:YES completion:^{[self dismissViewControllerAnimated:YES completion:nil];}]; 

感謝Michal的想法。

3

那是因爲你在做額外的視圖控制器。在按鈕所在的視圖控制器中創建MFMailComposeViewController。郵件撰寫控制器本身就是一個控制器。有一個白色屏幕,因爲這是您的VEMailView的默認視圖。擺脫,並把這些方法:

- (void)showMailPicker; 
- (void)displayMailComposerSheet; 

在視圖控制器與按鈕。也讓它成爲代表。

+0

這工作。但它不能自動工作。你給了我很好的想法,如何自動完成。我會在自己的答案中編寫解決方案。不管怎麼說,還是要謝謝你。 http://stackoverflow.com/a/23008141/1979882 – Vyacheslav