Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"unrecognized selector sent to instance" error in Objective-C

People also ask

What does unrecognized selector sent to instance mean?

Once you run this line of code you will instantly get an error message in the console area, which look s like this (unrecognized selector sent to instance). Since the selector is another name for the method, the error is basically saying that it doesn't recognize the function that you're trying to call on this object.

What is a selector in Xcode?

A selector is the name used to select a method to execute for an object, or the unique identifier that replaces the name when the source code is compiled.


It looks like you're not memory managing the view controller properly and it is being deallocated at some point - which causes the numberButtonClicked: method to be sent to another object that is now occupying the memory that the view controller was previously occupying...

Make sure you're properly retaining/releasing your view controller.


For those getting here via Google like I did, which probably pertains more to Xcode 4.2+/iOS 5+ more, what with ARC. I had the same error "unrecognized selector sent to instance". In my case I had a UIButton's target action set up to pass itself as the sender parameter, but later realised I didn't need it and removed that in code. So, something like:

- (IBAction)buttonPressed:(UIButton *)sender {

Was changed to:

- (IBAction)buttonPressed {

Right clicking the UIButton in question showed that the Touch Up Inside event was associated with the view controllers buttonPressed: method. Removing this and reassigning it to the modified method worked a treat.


This was the top Google answer for this issue, but I had a different cause/result - I thought I'd add in my two cents in case others stumble across this problem.

I had a similar issue just this morning. I found that if you right click the UI item giving you the issue, you can see what connections have been created. In my case I had a button wired up to two actions. I deleted the actions from the right-click menu and rewired them up and my problem was fixed.

So make sure you actions are wired up right.


OK, I have to chip in here. The OP dynamically created the button. I had a similar issue and the answer (after hours of hunting) is so simple it made me sick.

When using:

action:@selector(xxxButtonClick:)

or (as in my case)

action:NSSelectorFromString([[NSString alloc] initWithFormat:@"%@BtnTui:", name.lowercaseString])

If you place a colon at the end of the string - it will pass the sender. If you do not place the colon at the end of the string it will not, and the receiver will get an error if it expects one. It is easy to miss the colon if you are dynamically creating the event name.

The receiver code options look like this:

- (void)doneBtnTui:(id)sender {
  NSLog(@"Done Button - with sender");
}
 or
- (void)doneBtnTui {
  NSLog(@"Done Button - no sender");
}

As usual, it is always the obvious answer that gets missed.