MM.
MM.

Reputation: 4274

Cocoa touch - problem with button-viewController connection

I'm very new to cocoa touch and am stuck on a problem that doesn't seem to have a google solution. I have added a view to my application, and this view has a button on it. When the button is pressed I want the callback to perform an operation.

My problem is that the callback isn't being called. I created the view in Interface Builder, connected the touch-up-inside connection to my Owner class (in this case a viewController class), and selected the appropriate callback.

The error I get is as follows: 2009-10-13 17:13:51.708 MyApp[7467:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFSet contactsButtonPressed:]: unrecognized selector sent to instance 0x4c27330'

As I understand it this suggests that the connection between contactsButtonPressed and MyViewController is wrong. I'm not sure where the NSCFSet object comes from.

Does anyone have any idea?

Thanks!

Upvotes: 0

Views: 400

Answers (3)

Peter Hosey
Peter Hosey

Reputation: 96373

The error I get is as follows:

2009-10-13 17:13:51.708 MyApp[7467:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFSet contactsButtonPressed:]: unrecognized selector sent to instance 0x4c27330'

You're probably under-retaining whatever controller object of yours is supposed to receive that action message. Add an NSLog call in the controller's dealloc method; you'll probably find that it gets deallocked before you expect it to.

The question to ask then is what should own that controller. Then, make sure that all of the owners are retaining it.

If you're holding the controller in a property, make sure that you actually use that property. A common mistake is to write myController = theController, which bypasses the property and assigns directly to the instance variable, instead of self.myController = theController (property access syntax, implicit accessor message) or [self setMyController:theController] (explicit message syntax).

Also, if you've implemented your own accessors for the property (especially setMyController:), make sure your setter releases the old object and retains the new one. Of course, this is assuming you have a reason to implement your own accessor; normally, you should simply @synthesize the property and let the compiler write the accessor for you.

Upvotes: 2

coneybeare
coneybeare

Reputation: 33101

somewhere in your code you have a line that looks like this:

[button addTarget:self action:@selector(contactsButtonPressed:)];

If you have a line that looks like this, you will also need to have a method with this signature:

- (void)contactsButtonPressed:(UIButton *)sender {
  ...
}

If you look at the error, it seems that you are sending the message to an NSCFSet object instead of the controller. I would check that you are setting delegate to self or the controller.

Upvotes: 0

Prody
Prody

Reputation: 5298

I'm new to cocoa too,

Maybe you forgot to add the sender parameter to your contactsButtonPressed.

Is your view's class named NSCFSet?
Try pasting some code.

Upvotes: -1

Related Questions