1

MFMailComposeViewController嗨,我对委托属性感到困惑,当我mailer.mailComposeDelegate在调用后设置应用程序崩溃[self presentModalViewController:mailer animated:YES];时,当我这样做时,mailer.delegate应用程序不会崩溃,但它的视图在发送邮件后无法隐藏,或者只是从其导航 bat 按钮“取消”中取消它。我被卡住了为什么会发生这种情况。让我分享代码,你会得到提示我在哪里做错了。

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];仍然让我在加载图像时崩溃。这是我在崩溃后得到的图像。 在此处输入图像描述

4

4 回答 4

3

在 .h 文件中添加 MFMailComposeViewControllerDelegate

@interface VideoPlayAndSharing : UIViewController
<MFMailComposeViewControllerDelegate>  

显示作曲表

-(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视图没有隐藏的原因。

于 2012-11-21T10:40:11.653 回答
0

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

在 .m 中导入 MessageUI/MFMailComposeViewController.h,带有小于号和大于号,并添加 MessageUI.framework。

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

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

于 2012-11-21T10:45:29.423 回答
0

评论以下行[邮件发布];

我认为它导致了问题

于 2012-11-21T10:53:13.330 回答
0

检查标题 - 委托属性是类型id <UINavigationControllerDelegate>- 因为邮件作曲家继承自它,因此,由于作曲家的内部恶作剧,将其设置为某个东西可能是一个坏主意,以获取邮件作曲家状态的通知将自我设置为作曲家的 mailComposeDelegate。

于 2012-11-21T11:04:52.837 回答