iOS:MFMailComposeViewController无法处理模型

时间:2012-02-01 07:52:07

标签: iphone objective-c ios xcode mfmailcomposeviewcontroller

MFMessageComposeViewController出现问题。 情况是这样的:

我正在创建一个模型来实现MFMessageComposeViewController而不是直接在ViewController中执行它。现在,当我实现presentModalViewController ::方法时,它工作正常(出现mail.app接口)但是当我点击mail.app界面中的取消/发送按钮时,它不会关闭mail.app ..

就像这样:

来自MSGViewController的方法片段,用于实现发送邮件模型:

- (IBAction)openEmail:(id)sender {
    Messaging *mail = [[Messaging alloc]initWithModal:self];
    [mail emailInvitation:@"Eventz Date" eventAt:@"Eventz Location" withImage:nil];
    [self.sendingStatus setText:mail.sendingStatus];
}

我实施消息传递的模型:

Messaging.h

#import <Foundation/Foundation.h>
#import <MessageUI/MessageUI.h>

@interface Messaging : NSObject <MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>

@property (nonatomic, copy) NSString *sendingStatus;
@property (nonatomic, retain) id modal;

- (id)initWithModal:(id)modal;
- (void)emailInvitation:(NSString *)eventDate eventAt:(NSString *)eventLocation withImage:(UIImage *)imageAttachment;

@end

Messaging.m

#import "Messaging.h"

@implementation Messaging

@synthesize sendingStatus = _sendingStatus;
@synthesize modal = _modal;

- (id)initWithModal:(id)modal{
    self = [super init];
    self.modal = modal;
    return self;
}



- (void)emailInvitation:(NSString *)eventDate eventAt:(NSString *)eventLocation withImage:(UIImage *)imageAttachment{

    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

    if ([MFMailComposeViewController canSendMail]){      
        mailer.mailComposeDelegate = self;

        [mailer setSubject:@"Event Invitation"];
        [mailer setMessageBody:@"message body"] isHTML:NO];

        if(imageAttachment != nil){
            NSData *imageData = UIImagePNGRepresentation(imageAttachment);
            [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"imageFileName"];
        }

        [self.modal presentModalViewController:mailer animated:YES];
        return;
    }

    [self deviceDoNotSupportMessaging];        
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    NSLog(@"called");
    switch (result)
    {
        case MFMailComposeResultCancelled:
            self.sendingStatus = @"Mail sending cancelled.";
            break;
        case MFMailComposeResultSaved:
            self.sendingStatus = @"Mail saved.";
            break;
        case MFMailComposeResultSent:
            self.sendingStatus = @"Mail sent.";
            break;
        case MFMailComposeResultFailed:
            self.sendingStatus = @"Mail sending failed.";
            break;
        default:
            self.sendingStatus = @"Mail not sent.";
            break;
    }

    [controller dismissModalViewControllerAnimated:YES];
}
<> BTW,NSLog(@“叫”);在.m中实际上并未调用... 有人可以有任何建议吗?谢谢..:D

2 个答案:

答案 0 :(得分:6)

我想知道为什么当你的指针被dealloc'd时你没有收到错误。我使用你的代码,并在实例化Message类时立即得到了错误:

Messaging *mail = [[Messaging alloc]initWithModal:self];

所以我改变了一件事,我在我的通话类中邮寄了一个强大的财产:

@property (strong, nonatomic) Messaging *mail;

然后这样称呼它:

mail = [[Messaging alloc]initWithModal:self];
                [mail emailInvitation:@"Eventz Date" eventAt:@"Eventz Location" withImage:nil];
                NSLog(@"Called the emailInvitation");

调用了日志语句,我甚至确保MailComposer得到了正确的响应:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    NSLog(@"called");
    switch (result)
    {
        case MFMailComposeResultCancelled:
            self.sendingStatus = @"Mail sending cancelled.";
            NSLog(@"Mail Cancelled");
            break;
        case MFMailComposeResultSaved:
            self.sendingStatus = @"Mail saved.";
            NSLog(@"Mail Saved");
            break;
        case MFMailComposeResultSent:
            self.sendingStatus = @"Mail sent.";
            NSLog(@"Mail Sent");
            break;
        case MFMailComposeResultFailed:
            self.sendingStatus = @"Mail sending failed.";
            NSLog(@"Mail Failed");
            break;
        default:
            self.sendingStatus = @"Mail not sent.";
            NSLog(@"Mail Not Sent");
            break;
    }

    [controller dismissModalViewControllerAnimated:YES];
}

它确实如此。

顺便说一句 - 我喜欢你这样做的方式。这是OOP应该完成的方式。

答案 1 :(得分:1)

[self.modal dismissModalViewControllerAnimated:YES];

而不是[controller dismissModalViewControllerAnimated:YES];