Search code examples
iosutilitymfmailcomposer

Unrecognized selector sent to instance error from Utility App navigation controller view


I have a utility app. I've implemented this code on the flipside, which calls a Feedback view to send an email, like this tutorial. This works, but then when I click on the Send Feedback UIButton, my app immediately crashes with *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController sendMail]: unrecognized selector sent to instance 0x89c1960'.

I've checked these things:

I've declared the delegate properly and implemented it for the MailComposer.

My method, sendMail, is wired to the TouchUp Event for the button.

My method names agree:

- (IBAction)sendMail;

and

- (IBAction)sendMail 
{
if ([MFMailComposeViewController canSendMail]) 
{
    MFMailComposeViewController *mfViewController = [[MFMailComposeViewController alloc] init];
    mfViewController.mailComposeDelegate = self;

    [self presentModalViewController:mfViewController animated:YES];

}
else 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Status:" message:@"Your phone is not currently configured to send mail." delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];

    [alert show];
}

}

The code doesn't event get to this method because I have a breakpoint set at the top of the method implementation but doesn't get called. Nor did a breakpoint at ViewDidLoad get activated.

Looking at this error closely:

reason: '-[UIViewController sendMail]: unrecognized selector sent to instance 

it seems it is expecting a view controller named sendMail rather than a method. I read this post which seems very similar but I dont' see any other view controller names in the xib Identity drop down list. I think this is part of my problem but I'm not sure how to fix it.

Maybe I'm supposed to be presenting the MFMailComposer via a viewcontroller? If so, I'm not sure how to do that.

Any suggestions would be appreciated.


Solution

  • You incorrectly assigned the type UIViewController to your custom view-controller. You should select the class-type that actually applies to your custom view-controller (the one that holds the method implementation sendMail).

    The problem with your code / setup is that your custom view controller gets instantiated with the type UIViewController. But, UIViewController does not implement any method called sendMail, hence you get an exception.

    As you are not specifying the class name of your custom view controller, I will simply assume one for the sake for this answer; MyCustomViewController

    Since you appear to use the InterfaceBuilder for setting those things up, use it to change the type of your view-controller towards MyCustomViewController.

    enter image description here

    EDIT

    From your comments, I can see that you actually instantiate the view controller using code. In that case, replace your way with this:

    MyCustomViewController *controller = [[MyCustomViewController alloc] initWithNibName:@"ExMobSendFeedback" bundle:nil]; 
    controller.title = @"Feedback"; 
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];