Objective c 邮件撰写视图控制器赢得';当从NSObject调用时,不会关闭

Objective c 邮件撰写视图控制器赢得';当从NSObject调用时,不会关闭,objective-c,cocoa-touch,Objective C,Cocoa Touch,我有几个视图控制器需要发送电子邮件,所以为了保持面向对象,我创建了一个名为MessagingObject的NSObject类来处理这些消息。但我不知道如何消除MailComposeVC,因为它来自一个非VC对象。实现如下所示: //.m file #import "MessagingObject.h" #import <MessageUI/MessageUI.h> @interface MessagingObject () <MFMessageComposeViewContr

我有几个视图控制器需要发送电子邮件,所以为了保持面向对象,我创建了一个名为MessagingObject的NSObject类来处理这些消息。但我不知道如何消除MailComposeVC,因为它来自一个非VC对象。实现如下所示:

//.m file
#import "MessagingObject.h"
#import <MessageUI/MessageUI.h>

@interface MessagingObject () <MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate>

@end
@implementation MessagingObject


.....

#pragma mark - Email Messaging Methods

- (void)sendEmail:(NSString *)description withSubject:(NSString *)subj toRecipients:(NSArray *) recipients fromController:(id)sender{

    // Building the email content

     MFMailComposeViewController *mc = [[MFMailComposeViewController     alloc] init];
     mc.mailComposeDelegate = sender; //Delegated to the sending VC so it could bring out the composer
    [mc setSubject:subj];
    [mc setMessageBody:description isHTML:YES];
    [mc setToRecipients:recipients];


    // Present mail view controller on screen from the sender VC
    [sender presentViewController:mc animated:YES completion:NULL];
}


- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    //Check the result of the email being sent
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }
    // Close the Mail Interface here, this won't work with *self* (which references the MessagingObject)
    //[self dismissViewControllerAnimated:YES completion:NULL];

    //This delegate will notify the corresponding VC when the mail has been sent
    if (self.delegate && [self.delegate respondsToSelector:@selector(messageDidSend)]) {
        [self.delegate messageDidSend];
    }
}
@end
我尝试在头文件中使用名为MessengerDelegate的委托,并使用messageDidSend:方法通知VC何时关闭邮件组件。但是由于某种原因,mailComposeController did finishwithresult没有被调用,我认为这与我如何委托有关。不管怎么说,我怎么能拒绝MailComposeVC呢?

您有两个问题:

  • mailComposeDelegate
    设置为
    self
    ,而不是
    sender
    ,因为是
    self
    实现了委托方法

    mc.mailComposeDelegate = self;
    
  • 在委派方法中,在
    控制器上调用
    解除…

    [controller dismissViewControllerAnimated:YES completion:NULL];
    

  • 在实例变量中保存
    sender
    。然后,您可以在需要关闭邮件生成器时引用。只是尝试了一下,但在需要时它不会被关闭,这在mailComposeController didFinishWithResult方法中。无论出于何种原因,该方法正在被跳过。请注意,当我直接从视图控制器调用此代码时,此代码工作正常。当我将
    mailComposeDelegate
    设置为
    self
    时,它会在应用程序发送电子邮件之前崩溃,因此我无法判断在
    Controller
    上调用
    discouse…
    是否有效。当我将mailComposeDelegate设置为
    self
    时,它会使Mail Compose VC上的应用程序崩溃(更不用说到达
    didFinishWithResult'方法了),因此我无法判断在
    controller上调用
    Discouse…`是否可行。可能我缺少委托方法实现?实际上,问题是由于
    aMessenger
    sendmail:
    方法末尾超出了范围。使用实例变量而不是局部变量。好了!工作得很好!你帮我省去了头痛,谢谢!
    [controller dismissViewControllerAnimated:YES completion:NULL];