1789040
1789040

Reputation: 579

Undeclared identifier 'mailComposeController'

Here is my .m code:

- (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:@"[email protected]", @"[email protected]", 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];
        }

        }

}

I have imported everything correctly in .h and .m, but the email won't close... I have tried as suggested on Stack Overflow to fiddle with the mailer.mailComposeDelegate = self; but I am still getting an undeclared identifier error. How do I fix this? Thanks.

Upvotes: 1

Views: 1379

Answers (1)

Ramaraj T
Ramaraj T

Reputation: 5230

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

Move this function out of the tableview delegate method. You should learn some basics. You cannot have a method within another method. If you need then you can call the method by [self callingFunctionName];

In you case,

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:@"[email protected]", @"[email protected]", 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];
}
}

This will open the mail composer. When you send, cancel or save the mail, the delegate function

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

will automatically be called.

Upvotes: 2

Related Questions