“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:03

On my case I solved the problem after 2 hours :

The sender (a tabBar item) wasn't having any Referencing Outlet. So it was pointing nowhere.

Juste create a referencing outlet corresponding to your function.

Hope this could help you guys.

查看更多
梦醉为红颜
3楼-- · 2019-01-02 18:04

Including my share. I got stuck on this for a while, until I realized I've created a project with ARC(Automatic counting reference) disabled. A quick set to YES on that option solved my issue.

查看更多
余欢
4楼-- · 2019-01-02 18:05

Another really silly cause of this is having the selector defined in the interface(.h) but not in the implementation(.m) (p.e. typo)

查看更多
高级女魔头
5楼-- · 2019-01-02 18:06

I had the same issue. The problem for me was that one button had two Action methods. What I did was create a first action method for my button and then deleted it in the view controller, but forgot to disconnect the connection in the main storyboard in the connection inspector. So when I added a second action method, there were now two action methods for one button, which caused the error.

查看更多
看淡一切
6楼-- · 2019-01-02 18:07

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.

See Adam Rosenfield's answer here: Selectors in Objective C

查看更多
与风俱净
7楼-- · 2019-01-02 18:07

In my case I was using a UIWebView and I passed a NSString in the second parameter instead of a NSURL. So I suspect that wrong class types passed to a functions can cause this error.

查看更多
登录 后发表回答