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

时间:2012-11-21 10:35:16

标签: iphone ios xcode ios5 mfmailcomposeviewcontroller

您好我在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];发表评论,但仍然让我在加载图片时崩溃。 这是Image在崩溃后我得到的东西。 enter image description here

4 个答案:

答案 0 :(得分:3)

在.h文件中添加MFMailComposeViewControllerDelegate

@interface VideoPlayAndSharing : UIViewController
<MFMailComposeViewControllerDelegate>  

显示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视图没有隐藏。

答案 1 :(得分:0)

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

将.UI中的MessageUI / MFMailComposeViewController.h导入小于和大于符号,并添加MessageUI.framework。

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

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

答案 2 :(得分:0)

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

我认为这会导致问题

答案 3 :(得分:0)

检查标题 - delegate属性是id <UINavigationControllerDelegate>类型 - 因为邮件编辑器继承了它,所以,由于作曲家的内部恶作剧,设置这个可能是个坏主意,以获取邮件编辑器状态的通知将self设置为composer的mailComposeDelegate。