1

这是我的 .m 代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *emailCell = [tableView cellForRowAtIndexPath:indexPath];
    if ([emailCell.textLabel.text isEqualToString:@"Contact Developer"]) {
        NSLog(@"Email button pressed...");
        if([MFMailComposeViewController canSendMail]) {
            MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
            mailer.mailComposeDelegate = self;
            [mailer setSubject:@"A Message from MobileTuts+"];
            NSArray *toRecipients = [NSArray arrayWithObjects:@"fisrtMail@example.com", @"secondMail@example.com", nil];
            [mailer setToRecipients:toRecipients];
            NSString *emailBody = @"Have you seen the MobileTuts+ web site?";
            [mailer setMessageBody:emailBody isHTML:NO];
            [self presentModalViewController:mailer animated:YES];
            [mailer release];
            - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
            {
                switch (result)
                {
                    case MFMailComposeResultCancelled:
                        NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
                        break;
                    case MFMailComposeResultSaved:
                        NSLog(@"Mail saved: you saved the email message in the drafts folder.");
                        break;
                    case MFMailComposeResultSent:
                        NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
                        break;
                    case MFMailComposeResultFailed:
                        NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
                        break;
                    default:
                        NSLog(@"Mail not sent.");
                        break;
                }
                // Remove the mail view
                [self dismissModalViewControllerAnimated:YES];
            }
        }
        else {
            UIAlertView *emailAlert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                                 message:@"Please make sure you have an email address configured in your Mail app."
                                                                delegate:nil
                                                       cancelButtonTitle:@"Ok"
                                                       otherButtonTitles:nil];
            [emailAlert show];
            [emailAlert release];
        }

        }

}

我已在 .h 和 .m 中正确导入所有内容,但电子邮件不会关闭...我已尝试按照 Stack Overflow 上的建议来摆弄,mailer.mailComposeDelegate = self;但我仍然收到未声明的标识符错误。我该如何解决?谢谢。

4

1 回答 1

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

将此函数移出 tableview 委托方法。你应该学习一些基础知识。您不能在另一个方法中拥有一个方法。如果需要,可以通过以下方式调用该方法[self callingFunctionName];

在你的情况下,

if ([emailCell.textLabel.text isEqualToString:@"Contact Developer"]) {
    NSLog(@"Email button pressed...");
    [self sendEmail];
}

    -(void)sendEmail{
 if([MFMailComposeViewController canSendMail]) {
            MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
            mailer.mailComposeDelegate = self;
            [mailer setSubject:@"A Message from MobileTuts+"];
            NSArray *toRecipients = [NSArray arrayWithObjects:@"fisrtMail@example.com", @"secondMail@example.com", nil];
            [mailer setToRecipients:toRecipients];
            NSString *emailBody = @"Have you seen the MobileTuts+ web site?";
            [mailer setMessageBody:emailBody isHTML:NO];
            [self presentModalViewController:mailer animated:YES];
            [mailer release];
}
}

这将打开邮件编辑器。当您发送、取消或保存邮件时,委托功能

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

会自动调用。

于 2012-11-06T05:41:26.233 回答