嗨,我对MFMailComposeViewController委托属性感到困惑,当我在调用mailer.mailComposeDelegate后立即设置[self presentModalViewController:mailer animated:YES];应用崩溃,当我执行mailer.delegate时应用不会崩溃,但在发送邮件后无法隐藏其视图,或者只是从其导航蝙蝠按钮中将其取消“取消”。我很难理解为什么会这样。
让我共享代码,您会得到我在哪里做错的提示。

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>

显示作曲表
-(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:@"[email protected]"];
        NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
        NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"];

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

关于iphone - mailComposeDelegate和简单的Delegate属性之间的区别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13491345/

10-09 01:48