2012-07-11 42 views
6

我正在嘗試截圖並使用郵件編輯器通過電子郵件發送郵件。一切都很好,除了郵件作曲家不會放棄。這篇文章似乎有同樣的問題,但提供的解決方案並不適合我。 Can't dismiss the email composer view in iPhone?iOS郵件編輯器不會消除

- (IBAction)Email:(id)sender { 
UIGraphicsBeginImageContext(self.view.frame.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

NSData * imageData = UIImageJPEGRepresentation(image, 1.0); 

if ([MFMailComposeViewController canSendMail]) { 
    MFMailComposeViewController * mailComposer = [[[MFMailComposeViewController alloc] init] autorelease]; 
    mailComposer.delegate = self; 
    [mailComposer setSubject:@"Risk Assessment"]; 
    [mailComposer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"attachment.jpg"];  
    [self presentModalViewController:mailComposer animated:YES];   
} 
} 

上面的代碼很好用。我如何稱此底部。編譯器似乎只是跳過它而已。

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ 
if (error){ 
    NSString *errorTitle = @"Mail Error"; 
    NSString *errorDescription = [error localizedDescription]; 
    UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:errorTitle message:errorDescription delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
    [errorView show];     
    [errorView release]; 
} 
[controller dismissModalViewControllerAnimated:YES]; 

} 

在此先感謝。

回答

17

嘗試

mailComposer.mailComposeDelegate = self; 

代替

mailComposer.delegate = self; 

MFMailComposeViewController documentation

@property(nonatomic,assign) id<MFMailComposeViewControllerDelegate> mailComposeDelegate; 

的委託對象負責駁回在此視圖控制器呈現的視圖適當的時候。因此,您應始終提供一個委託,並且該對象應實現MFMailComposeViewControllerDelegate協議的方法。

+0

感謝您的快速響應。這工作。 – talbright 2012-07-11 14:33:08

+0

好的。從未見過。 – 2012-07-11 14:49:04

+0

數小時試圖找到我的問題,這固定它! – Mark 2013-11-22 16:07:32

2

我敢肯定,最後一行應

[self dismissModalViewControllerAnimated:YES]; 

的ViewController中所呈現的觀點模態,也將其關閉。

+0

這是正確的,他寫的方式也應該工作,但首選的方式是你所描述的[Apple Docs on Dismissing](http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ ModalViewControllers.html#// apple_ref/DOC/UID/TP40007457-CH111-SW14) – Bersaelor 2012-07-11 14:36:14

相關問題