views:

239

answers:

4

Am tearing my hair out here. Have read many examples which describe how to get around this but none have helped me get rid of the problem.

Have a simple button on a UIView linked to an IBAction.

Code is this...

Contact.h

#import <UIKit/UIKit.h>

@interface Contact : UIViewController {
}

-(IBAction)buttonPressed:(id)sender;

@end

Contact.m

#import "Contact.h"

@implementation Contact

- (IBAction)buttonPressed:(id)sender  
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Button Pressed" message:@"You pressed the button" delegate:nil cancelButtonTitle:@"Yep, I did." otherButtonTitles:nil];
    [alert show];
    [alert release];
}

Constantly receiving this error message :

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIViewController buttonPressed:]: unrecognized selector sent to instance 0xd1d5f0'

Touch Up Inside is linked to the file's owner using buttonPressed.

Have downloaded other example code and they work fine. My project and nib are set up identically to the other example code.

Haven't got a clue where to even start to try and debug this.

Anyone help?

A: 

Are you sure you pasted the right code? Your Contact.m should contain a @implementation block containing the implementation, not a @interface block containing the definitions

Contact.m:

#import "Contact.h"

@implementation Contact

- (IBAction)buttonPressed:(id)sender
{
    NSLog(@"buttonPressed");
}

@end
MathieuK
oops, you are right. have now pasted it as it should have been :)
iphone_developer
am also building a brand new project from scratch with one button and one IBAction, no other code and it is still giving me the same headache :(
iphone_developer
+1  A: 

In Interface Builder, you need to tell it what class File's Owner is. Click the "File's Owner" icon, go to the Identity pane in the Inspector (the 'i' icon at the end) and change the Class to Contact.

iKenndac
Hi, I've done that. Also, just created a brand new project with nothing inside it other than a UIView and a button and it is still happening :(
iphone_developer
Are you *sure*? This part of the error: `'*** -[UIViewController buttonPressed:]: unrecognized selector sent to instance 0xd1d5f0'` tells you that a UIViewController instance is receiving the message, NOT a Contact instance.
iKenndac
+1 I thought for sure this was the right answer.
e.James
A: 

just created a brand new project with nothing inside it other than a UIView and a button and it is still happening

This makes me think that you are not quite setting things up properly. If this is the application nib file, and it contains nothing but a UIView, then the File's Owner will be your application delegate, regardless of what you set the type of file's owner to be in IB.

Try the following:

  1. Create an instance of Contact in your nib file: drag in a UIViewController object, and set its class to Contact.

  2. Wire up the view and the button up to your Contact instance.

  3. Let us know if that works :)

e.James
Created a new project and it worked fine. I think it may be something to do with the fact that it is a view which is loaded from a TabBarController.The NIB itself is set up with the correct IBAction and class but I can't seem to work out where the 'unrecognised selector' is being sent to.Thanks to everyone who helped, I'll soldier on until I find out what it is I'm doing wrong :)
iphone_developer
+1  A: 

I had this exact same problem. It's because your ViewController was under a TabBarController and in the View Controller under it, you set the NIB Name to Contact but did not set the Class to Contact in the identity pane.

bertrandom
yep, this was it :)
iphone_developer