“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 17:55

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.

查看更多
君临天下
3楼-- · 2019-01-02 17:55

This can also happen if you don't set the "Class" of the view in interface builder.

查看更多
无色无味的生活
4楼-- · 2019-01-02 17:58

Another possible solution: Add '-ObjC' to your linker arguments.

Full explanation is here: Objective-C categories in static library

I think the gist is: if the category is defined in a library you are statically linking with, the linker isn't smart enough to link in category methods. The flag above makes the linker link in all objective C classes and categories, not just ones it thinks it needs to based on analyzing your source. (Please feel free to tune or correct that answer. I'm knew to linked languages, so I'm just parroting here).

查看更多
冷夜・残月
5楼-- · 2019-01-02 18:01

I had a similar problem, but for me the solution was slightly different. In my case, I used a Category to extend an existing class (UIImage for some resizing capabilities - see this howto in case you're interested) and forgot to add the *.m file to the build target. Stupid error, but not always obvious when it happens where to look. I thought it's worth sharing...

查看更多
无色无味的生活
6楼-- · 2019-01-02 18:02

Happened to me because of conflicting constraint arguments.

查看更多
萌妹纸的霸气范
7楼-- · 2019-01-02 18:03

..And now mine

I had the button linked to a method which accessed another button's parameter and that worked great BUT as soon I tried to do something with the button itself, I got a crash. While compiling, no error has been displayed.. Solution?

I failed to link the button to the file's owner. So if anyone here is as stupid as me, try this :)

查看更多
登录 后发表回答