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
        {

この関数をテーブルビューデリゲートメソッドの外に移動します。あなたはいくつかの基本を学ぶべきです。別のメソッド内にメソッドを含めることはできません。必要に応じて、次の方法でメソッドを呼び出すことができます。[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 に答える