2013-07-05 65 views
2

我用MFMailComposeViewController發送郵件給他人。當點擊一個按鈕時,撰寫工作表正在打開,我可以輸入地址,主題,郵件正文。但是,點擊發送按鈕後,郵件頁面不會關閉。MFMailComposeViewController not dismiss

代碼:

if ([MFMailComposeViewController canSendMail]) 
{ 
    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init]; 
    mailer.mailComposeDelegate = self; 
    [mailer setSubject:@"My image"]; 


    UIImage *myImage = [UIImage imageNamed:@"mobiletuts-logo.png"]; 
    NSData *imageData = UIImagePNGRepresentation(myImage);   


    [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"Image"]; 
    NSString *emailBody = @"Hi, my image"; 
    [mailer setMessageBody:emailBody isHTML:NO];   

    [self presentViewController:mailer animated:YES completion:nil]; 
} 

- (void)mailComposeController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result 
{ 
    switch (result) { 
     case MFMailComposeResultCancelled: 
      break; 
     case MFMailComposeResultSent: 
      break; 
     default: 
      break; 
    } 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 
+1

希望妳的.h文件中添加'MFMailComposeViewControllerDelegate'協議。 – HRM

+0

是的。我補充說。但它不起作用 – user2474320

+0

電話簿看起來很奇怪,bcoz我在代碼中找不到任何其他錯誤。你是否檢查委託方法是否被調用? – HRM

回答

2

對不起。忘記添加這些行。在此之後行郵寄工作

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

試試這個

[self presentModalViewController:mailer animated:YES]; 

因解除

[self dismissModalViewControllerAnimated:YES];