Rectangle 27 118

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 {
- (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 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!

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.

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 118

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 {
- (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 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!

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.

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 164

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

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

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

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

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

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

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 164

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

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

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

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

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

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

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 61

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.

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.

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

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 61

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.

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.

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

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 24

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.

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.

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

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!

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

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

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 24

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.

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.

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

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!

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

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

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 19

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.

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

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.

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.

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

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 19

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.

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

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.

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.

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

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 12

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.

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 12

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.

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 12

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.

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 12

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.

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 11

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

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

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).")
    }

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

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 11

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

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

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).")
    }

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

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 4

That didn't work, so I went and re-created the subclass through XCode, which is when I started getting this error ("unrecognized selector sent to instance" when setting a value for the attribute).

created a new version of the Core Data Model via XCode

then re-created the NSManagedObject subclass

It was probably creating a new data model and the new subclass based on that, so I probably didn't need to clean or delete derived data...but it didn't hurt, either!

I had a similar issue that I was able to fix just by changing the current model version, and then changing it back. I also did a clean, though that by itself didn't help.

iphone - CoreData issue: -[NSManagedObject setValue:]: unrecognized se...

iphone objective-c core-data ios nsmanagedobject
Rectangle 27 4

That didn't work, so I went and re-created the subclass through XCode, which is when I started getting this error ("unrecognized selector sent to instance" when setting a value for the attribute).

created a new version of the Core Data Model via XCode

then re-created the NSManagedObject subclass

It was probably creating a new data model and the new subclass based on that, so I probably didn't need to clean or delete derived data...but it didn't hurt, either!

I had a similar issue that I was able to fix just by changing the current model version, and then changing it back. I also did a clean, though that by itself didn't help.

iphone - CoreData issue: -[NSManagedObject setValue:]: unrecognized se...

iphone objective-c core-data ios nsmanagedobject
Rectangle 27 5

The most obvious cause of this (included for completeness) is improperly casting a pointer and calling a method of the wrong class.

NSArray* array = [[NSArray alloc] init];
[(NSDictionary*)array objectForKey: key]; // array is not a dictionary, hence exception

This is a good hint - not subclassing a view or data object (eg by failing to add subclass name) is another way this happens

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton
Rectangle 27 5

The most obvious cause of this (included for completeness) is improperly casting a pointer and calling a method of the wrong class.

NSArray* array = [[NSArray alloc] init];
[(NSDictionary*)array objectForKey: key]; // array is not a dictionary, hence exception

This is a good hint - not subclassing a view or data object (eg by failing to add subclass name) is another way this happens

iphone - unrecognized selector sent to instance - Stack Overflow

iphone uiviewcontroller uibutton