iOS Mail Composer不会被忽略

时间:2012-07-11 14:26:46

标签: objective-c ios xcode email

我正在尝试截取屏幕截图并使用邮件编辑器发送电子邮件。一切都很好,除了邮件作曲家不会被解雇。这篇文章似乎有同样的问题,但提供的解决方案对我不起作用。 Can't dismiss the email composer view in iPhone?

- (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];

}

提前致谢。

2 个答案:

答案 0 :(得分:17)

尝试

mailComposer.mailComposeDelegate = self;

而不是

mailComposer.delegate = self;

来自MFMailComposeViewController documentation

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

委托对象负责在适当的时间解除此视图控制器提供的视图。因此,您应始终提供委托,该对象应实现MFMailComposeViewControllerDelegate协议的方法。

答案 1 :(得分:2)

我很确定最后一行应该是

[self dismissModalViewControllerAnimated:YES];

以模态方式呈现视图的ViewController也将其解除。