2

我有一个调用 MFMailComposeViewController 的按钮。有时,当我单击“删除草稿”时,应用程序会崩溃,但有时它会正确关闭视图。我不明白为什么。这是我的代码:

- (IBAction)openEmail:(id)sender {
if ([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
    composer.mailComposeDelegate = (id)self;
    NSArray *myEmail = [[NSArray alloc] initWithObjects:@"me@gmail.com", nil];
    [composer setToRecipients:myEmail];
    [self presentViewController:composer animated:YES completion:nil];
    [[composer navigationBar] setTintColor:[UIColor colorWithRed:0.843 green:0.435 blue:0.435 alpha:1]];
}
else {

   }
}

这个方法:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
switch (result)
{
    case MFMailComposeResultCancelled:
        NSLog(@"Mail cancelled");

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

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

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

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

        break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}

谁能帮我?

4

2 回答 2

1

我曾经也遇到过这种情况,您的代码中潜在的问题区域可能是这一行:

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

很可能在MFMailComposeViewControllerDelegate调用方法时,您的composer对象已被释放。相反,尝试启动MFMailComposeViewController一个强属性,如下所示:

self.composer = [MFMailComposeViewController new];

一旦您self.composernil委托方法采取行动以释放MFMailComposeViewController.

于 2015-03-06T15:37:16.513 回答
0

你有没有尝试过:

[self presentModalViewController:_picker animated:YES];

注意“模式”?

还有:

[self dismissModalViewControllerAnimated:YES];
于 2013-05-20T11:56:43.873 回答