Rectangle 27 1

iphone unrecognized selector sent to instance?


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

or (as in my case)

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

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

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.

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.

Thank you SO very much! This was my problem (not putting a colon ':' at the end of the name of the selector). How come it is almost always the most stupid simple things that have us geeks banging our heads against the wall at 3 a.m. in the morning? Really THANK YOU!

This seems to have been my problem as well, thanks.

hahahaha.. I have the same problem! but it's 4am here :)

Note
Rectangle 27 1

iphone unrecognized selector sent to instance?


-(IBAction)numberButtonClick:(id)sender{

Please check what signature for @selector is expected. Usually it is, as you described in the code

The objective C is a dynamic language which invokes methods by looking at runtime for methods in classes. For example, if there is Class A including method DoSomething(arg1, arg2). If you try to call the method using correct arguments on an object of Class A everything will work fine. However, if the arguments are not passes properly, for example, you called the method with one argument only then the the run-time will treat this as a method call for a different method. Eventually, run-time will fail to find the method (DoSomething with one argument) and will through this exception "unrecognized selector sent to instance".

Note
Rectangle 27 1

iphone unrecognized selector sent to instance?


@alex gray, What? Proper memory management on the view controller solved the problem... I don't understand how that is not a solution?

It is immediately obvious. Your NSWindow memory management is not correct somewhere along the line. Go through it's life cycle and correct it.

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...

OK, so problem identified... but "Make sure you're properly retaining/releasing your view controller." is a suggestion, not a solution.

That's a very astute diagnosis from a very few lines of code.

correct. although this does seem to be "the fix".. and i may be in the minority, (though i still manage to exist), but the connection between the problem and the solution is not immediately obvious. for example, I am encountering -[NSWindow end]: unrecognized selector sent to instance 0x100523e80 and although i realize the similarities to the OP, and even possibly the cause of problem from your answer, the implementation of a fix is not immediately obvious

Note
Rectangle 27 1

iphone unrecognized selector sent to instance?


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

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:

I was just struggling with a similar issue, removed the sender parameter from the selector function, and then wondered how to get the sender. In my case, I was getting the error because I forgot to put a colon after the selector callback, thus causing a calling sequence mismatch. With the colon, I can keep the sender argument.

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 very helpful, although I think its own question and response would make it easier to find.

Very helpful response. I just ran into this exact problem and your solution was exactly right. Was following the Stanford iOS class on iTunesU when this happened. Thanks again for your answer!

Note
Rectangle 27 1

iphone unrecognized selector sent to instance?


In my case, I was using NSNotificationCenter and was attempting to use a selector that took no arguments, but was adding a colon. Removing the colon fixed the problem.

When using a selector name, don't use a trailing colon if there are no arguments. If there's one argument, use one trailing colon. If there are more than one argument, you must name them along with a trailing colon for each argument.

Note
Rectangle 27 1

iphone unrecognized selector sent to instance?


One of the times where I wish I could double vote!

Why does it need to know the class? I'm trying to use a UITableViewSection as a UIView, and so I'm getting this error. The compiler says that UITableViewSection doesn't exist so I presume its internal to UIKit.

Note
Rectangle 27 1

iphone unrecognized selector sent to instance?


Here is an image of my button configuration (right click on the button to view it):

In my case the function was not expecting an argument but the button was configured to send one causing the error. To fix this I had to rewire the event handler.

Next I had to reconnect the button to the event. To do this hold down the Control key and then drag a line from the button to the action. It should say "Connect Action". Note: I had to restart XCode for this to work for some reason; otherwise it only let me insert actions (aka create a new action) above or below the function.

Notice there are 3 event handlers.

To fix this I had to remove each of the event items since one of them was sending a reference to itself to the enterPressed function. To remove these items I clicked on the little x icon next to the name of each item until there were no items shown.

You should now have a single event handler wired to the button event that passes no arguments:

Note
Rectangle 27 1

iphone unrecognized selector sent to instance?


var trashBarButtonItem: UIBarButtonItem {
        return UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "newButtonClicked:")
    }

    func newButtonClicked(barButtonItem: UIBarButtonItem) {
        NSLog("A bar button item on the default toolbar was clicked: \(barButtonItem).")
    }

I had this problem with a Swift project where I'm creating the buttons dynamically. Problem code:

Just after following the tutorial and reading a hint about this exact same issue - I fell for it :D

The solution was to add a full colon ':' after the action: e.g.

Note
Rectangle 27 1

iphone unrecognized selector sent to instance?


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

or (as in my case)

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

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

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.

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.

Thank you SO very much! This was my problem (not putting a colon ':' at the end of the name of the selector). How come it is almost always the most stupid simple things that have us geeks banging our heads against the wall at 3 a.m. in the morning? Really THANK YOU!

This seems to have been my problem as well, thanks.

hahahaha.. I have the same problem! but it's 4am here :)

Note
Rectangle 27 1

iphone unrecognized selector sent to instance?


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.

THANK YOU! It turned out that I had three methods which I had deleted still referenced. Bothering me for a week!

Thanks a lot for this answer, it helped me out greatly!

This was my problem. I renamed a function and changed my code but didn't change the connection in the xib file.

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.

Note