mailComposeDelegate和简单的Delegate属性之间的区别

您好,我在MFMailComposeViewController委托属性,当我设置mailer.mailComposeDelegate应用程序崩溃后调用[self presentModalViewController:mailer animated:YES];混淆[self presentModalViewController:mailer animated:YES]; 当我做mailer.delegate然后应用程序不会崩溃,但它的观点不能在发送邮件后隐藏或只是取消它的导航蝙蝠button“取消”。 我陷入了为什么会发生这种情况。 让我分享代码,你会得到暗示我在做错的地方。

 if ([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init]; if(mailer) { mailer.mailComposeDelegate = self; //mailer.delegate=self; [mailer setSubject:@"What the Buck?"]; imageData = UIImagePNGRepresentation(screenImgSubCat); [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"testapp"]; NSString *emailBody = @"What the Buck?! – www.testapp.com"; [mailer setMessageBody:emailBody isHTML:NO]; [self presentModalViewController:mailer animated:YES]; //[mailer release]; } } } 

更新

我改变代码并使用mailer.mailComposeDelegate = self; 并发表评论这一行[mailer release]; 当图像被加载时仍然让我崩溃。 这是图像我得到后崩溃。 在这里输入图像说明

在.h文件中添加MFMailComposeViewControllerDelegate

 @interface VideoPlayAndSharing : UIViewController <MFMailComposeViewControllerDelegate> 

Display ComposerSheet

 -(void)displayComposerSheet { if ((videodta.length/1024)/1024 < 25) { NSLog(@"Video size >> %d",videodta.length/1024); MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setSubject:@"Your subject"]; // Set up recipients NSArray *toRecipients = [NSArray arrayWithObject:@"rajneesh071@gmail.com"]; NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil]; NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"]; [picker setToRecipients:toRecipients]; [picker setCcRecipients:ccRecipients]; [picker setBccRecipients:bccRecipients]; [picker addAttachmentData:videodta mimeType:@"video/mp4" fileName:@"MyPersonalMessage"]; // Fill out the email body text NSString *emailBody = @"Type your message here"; [picker setMessageBody:emailBody isHTML:NO]; [self presentModalViewController:picker animated:YES]; } else{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Personal Message" message:@"Video exceed the limit of 25 MB" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } } 

和委托方法

 - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { // Notifies users about errors associated with the interface switch (result) { case MFMailComposeResultCancelled: message.text = @"Result: canceled"; break; case MFMailComposeResultSaved: message.text = @"Result: saved"; break; case MFMailComposeResultSent: message.text = @"Result: sent"; break; case MFMailComposeResultFailed: message.text = @"Result: failed"; break; default: message.text = @"Result: not sent"; break; } [self dismissViewControllerAnimated:YES completion:nil]; } 

编辑

picker.mailComposeDelegate它的MFMailComposeViewControllerDelegate委托

它响应- (void)mailComposeController

picker.delegate它的UINavigationControllerDelegate委托

它的响应导航控制器不- (void)mailComposeController ,所以取消点击它不会调用,这就是为什么你的MFMailComposeViewController视图不隐藏。

注释以下行[邮件发布];

我认为这造成了问题

检查标头 – 委托属性的types为id <UINavigationControllerDelegate> – 因为邮件编辑器是从它inheritance的,所以,把它设置为某个东西可能是一个坏主意,因为作曲者的内部神秘,要得到邮件composer php状态设置自我作为作曲者mailComposeDelegate。

添加MFMailComposeViewControllerDelegate委托在你的.h文件中,然后再次检查。mailer.mailComposeDelegate = self; 是正确的方式不mailer.delegate =自我也改变它在你的代码,然后检查。

使用less于和大于符号导入Message.UI / MFMailComposeViewController.h这个.m,并添加MessageUI.framework。

 NSArray *toRecipients = [NSArray arrayWithObject:@"vishal@ldh.01s.in"]; [mail setToRecipients:toRecipients]; 

在设置消息正文之前添加这两行然后检查。