2

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.

Community
  • 1
  • 1
Jazzmine
  • 1,837
  • 8
  • 36
  • 54

1 Answers1

3

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];
Till
  • 27,559
  • 13
  • 88
  • 122
  • Yes, I am using IB to set everything up. I don't see my viewcontroller name (XMSendMail) in the list of Custom Class classes in the on the Identity tab while I have my view highlighted. Is there something else I'm supposed to do to get the viewcontroller listed there? BTW, I created the XMSendMail viewcontroller by adding new files. Thanks – Jazzmine Dec 06 '11 at 12:26
  • Just type it into the field named: Class under Custom Class. – Till Dec 06 '11 at 12:31
  • That didn't work, it beeped and didn't accept the name. The viewcontroller is created programmtically via this: UIViewController *controller = [[UIViewController alloc] initWithNibName:@"ExMobSendFeedback" bundle:nil]; so maybe that's why it's not showing up in the class list. However, the files (.h, .m, .xib) do exist now. BTW, thanks for working with me on this. controller.title = @"Feedback"; [self.navigationController pushViewController:controller animated:YES]; – Jazzmine Dec 06 '11 at 12:37
  • Ok progress. I took your modificaiton and changed it in the program and imported the header file for it and it produced no errors. However, I noticed that the viewcontroller name still didn't show up in the class list. That's when I noticed I didn't really have a viewcontroller in my list of objects in the xib, just the view and the button. So I added a viewcontroller, saw the ExMobSendMail name there and ran the app. It worked like a champ - Thanks Till for your help. – Jazzmine Dec 06 '11 at 12:54