0

我希望能够从我的应用程序发送一封电子邮件,其中我已经让那部分工作。问题是当我取消发送电子邮件(这是我现在正在测试的)时,电子邮件部分被关闭,但我留下了一个我似乎无法关闭的黑屏。

所以这就是我所拥有的。我创建了一个类来处理电子邮件部分:

MailViewController.h

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

@interface MailViewController : UIViewController <MFMailComposeViewControllerDelegate>


@end

实现如下:

#import "MailViewController.h"

@interface MailViewController ()

@end

@implementation MailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    if([MFMailComposeViewController canSendMail]){
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        [mailer setMailComposeDelegate:self];

        [mailer setSubject:@"Subject"];
        NSMutableArray *toArray = [[NSMutableArray alloc] initWithObjects:@"email@gmail.com", nil];
        [mailer setToRecipients:toArray];

        [self presentViewController:mailer animated:YES completion:nil];

    }
    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failuer" message:@"This device is unable to send emails" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{

    switch (result){
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email was sent");
            break;

        case MFMailComposeResultFailed:
            NSLog(@"Mail failed");
            break;

        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;

        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;

        default:
            NSLog(@"Mail not sent");
            break;
    }

    [controller dismissViewControllerAnimated:YES completion:nil];
}


@end

所有这些都是从另一个视图显示的,如下所示:

-(IBAction)sendMessage:(id)sender{
    NSLog(@"Going to send an email....");

    MailViewController *mail = [[MailViewController alloc] init];

    [self.navigationController pushViewController:mail animated:YES];


}

所以当我运行所有这些时,电子邮件程序会打开,我可以处理电子邮件。我可以选择取消,然后选择删除草稿,然后删除电子邮件程序。但是,我留下了一个黑屏,然后我可以从顶部导航栏中选择返回以返回到上一个视图。

我只是希望应用程序在发送或取消电子邮件(或其他)时返回显示电子邮件程序的视图。我敢肯定这是我想念的简单的东西。

4

2 回答 2

1

问题在于您是pushingMailViewController,然后dimissing,如果push您应该pop使用视图控制器。

MFMailComposeViewControllers 应该以模态方式呈现,而不是推送到导航堆栈上。您也不需要子类,您可以MFMailComposeViewController直接创建一个实例:

-(IBAction)sendMessage:(id)sender{
    NSLog(@"Going to send an email....");

    MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
    mail.mailComposeDelegate = self;
    [self presentViewController:mail animated:YES completion:nil];
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{

    switch (result){
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email was sent");
            break;

        case MFMailComposeResultFailed:
            NSLog(@"Mail failed");
            break;

        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;

        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;

        default:
            NSLog(@"Mail not sent");
            break;
    }

    [controller dismissViewControllerAnimated:YES completion:nil];
}
于 2012-10-18T19:41:30.797 回答
0

我有几个架构问题,但首先 - TL;DR 版本:

[self.navigationController popViewControllerAnimated:NO];

您也可以为布尔值选择 YES,但根据您的实现来判断,这不会产生好的结果。把它放在委托调用中以完成。

然而,为什么不让任何将 MailViewController 推入堆栈的东西来处理成为 MFMailComposeViewControllerDelegate 的工作呢?您在这里没有做任何需要将一个全新的视图控制器推入堆栈的操作。

于 2012-10-18T19:39:03.070 回答