“unrecognized selector sent to instance” error in

2019-01-02 17:20发布

I created a button and added an action for it, but as soon as it invoked, I got this error:

-[NSCFDictionary numberButtonClick:]: unrecognized selector sent to instance
 0x3d03ac0 2010-03-16 22:23:58.811
 Money[8056:207] *** Terminating app
 due to uncaught exception
 'NSInvalidArgumentException', reason:'*** -[NSCFDictionary numberButtonClick:]:  unrecognized selector sent to instance 0x3d03ac0'

This is my code:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        UIButton *numberButton = [UIButton buttonWithType:UIButtonTypeCustom];        
        numberButton.frame = CGRectMake(10, 435, 46, 38);
        [numberButton setImage:[UIImage imageNamed:@"one.png"] forState:UIControlStateNormal];
        [numberButton addTarget:self action:@selector(numberButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview: numberButton]; 
    }
return self;
}

-(IBAction)numberButtonClick:(id)sender{
    NSLog(@"---");
}

30条回答
若你有天会懂
2楼-- · 2019-01-02 18:18

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.

查看更多
宁负流年不负卿
3楼-- · 2019-01-02 18:18

I think you should use the void, instead of the IBAction in return type. because you defined a button programmatically.

查看更多
ら面具成の殇う
4楼-- · 2019-01-02 18:18

I'm currently learning iOS development and going through the "Beginning iOS6 Development" book by aPress. I was getting the same error in Chapter 10:Storyboards.

It took me two days to figure it out but found out I accidentally set the TableView cell's tag to 1 when I shouldn't have. For anyone else doing this book and receive a similar error I hope this helps.

I really hope future errors in my code are easier to find! hahaha. The debug error did nothing to push me in the right direction to figuring it out (or at least I'm too new to understand the debugger, lol).

查看更多
素衣白纱
5楼-- · 2019-01-02 18:18

My problem and solution was different and I thought I should post it here so that future readers can save their head from banging to the wall.

I was allocating different xib to same UIVIewController and even after searching everywhere I couldn't find how to correct it. Then I checked my AppDelegate where I was calling initWithNibName and can see that while copying the code, I changed the xib name, but forgot to change UIViewController class. So if none of the solution works for you, check your initWithNibName method.

查看更多
骚的不知所云
6楼-- · 2019-01-02 18:20

I had the same error and I discovered the following:

When you use the code

[self.refreshControl addTarget:self action:@selector(yourRefreshMethod:) forControlEvents:UIControlEventValueChanged];

You may think it's looking for the selector:

- (void)yourRefreshMethod{
    (your code here)
}

But it's actually looking for the selector:

- (void)yourRefreshMethod:(id)sender{
    (your code here)
}

That selector doesn't exist, so you get the crash.

You can change the selector to receive (id)sender in order to solve the error.

But what if you have other functions that call the refresh function without providing a sender? You need one function that works for both. Easy solution is to add another function:

- (void)yourRefreshMethodWithSender:(id)sender{
    [self yourRefreshMethod];
}

And then modify the refresh pulldown code to call that selector instead:

[self.refreshControl addTarget:self action:@selector(yourRefreshMethodWithSender:) forControlEvents:UIControlEventValueChanged];

I'm also doing the Stanford iOS course on an older Mac that can't be upgraded to the newest version of Mac OSX. So I'm still building for iOS 6.1, and this solved the problem for me.

查看更多
笑指拈花
7楼-- · 2019-01-02 18:21

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 my function:

enter image description here

Notice it contains no arguments.

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

enter image description here

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.

enter image description here

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.

enter image description here

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

enter image description here

This answer compliments the answer by @Leonard Challis which you should read as well.

查看更多
登录 后发表回答