"unrecognized selector sent to instance" error in Objective-C
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.
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.
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.
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.