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

Yet another slightly different solution/case.

I am using Xamarin and MvvmCross and I was trying to bind the UIButton to a ViewModel. I had the UIButton wired up to an Outlet and a TouchUpInside.

When Binding I only use the Outlet:

set.Bind (somethingOutlet).For ("TouchUpInside").To(vm => vm.Something);

All I had to do was remove the action (TouchUpInside) connection in XCode and that solved it.

P.S. I guess this is in its base all related to the previous answers and to @Chris Kaminski in particular, but I hope this helps someone...

Cheers.

查看更多
旧人旧事旧时光
3楼-- · 2019-01-02 18:08

Another reason/solution to add to the list. This one is caused by iOS6.0 (and/or bad programming). In older versions the selector would match if the parameter types matched, but in iOS 6.0 I got crashes in previously working code where the name of the parameter wasn't correct.

I was doing something like

[objectName methodName:@"somestring" lat:latValue lng:lngValue];

but in the definition (both .h and .m) I had

(viod) methodName:(NSString *) latitude:(double)latitude longitude:(double)longitude;

This worked fine on iOS5 but not on 6, even the exact same build deployed to different devices.

I don't get why the compiler coudn't tell me this, anyway - problem soled.

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

This also might happen when you want to set a property from a ControllerA to a public property inside a custom ControllerB class and you haven't set the "Custom Class" inside the identity inspector in storyboards yet.

查看更多
只若初见
5楼-- · 2019-01-02 18:10

I'm replying to Leonard Challis, since I was also taking the Stanford iOS class C193P, as was user "oli206"

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:"

The problem was that I had the "Enter" button on the calculator connected twice,and a friend pointed out that doing an inspection of the button in the Storyboard showed that 2 entries were on the "Touch Up Inside" attributes when I right clicked on the "Enter" button. Erasing one of the two "Touch Up Inside" "Sent Events" solved the problem.

This showed that the problem is triggered (for the C193P video class on the Calculator Walkthrough on Assignment 1) as 2 sent events, one of which was causing the exception.

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

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
查看更多
若你有天会懂
7楼-- · 2019-01-02 18:12

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 {

Was changed to:

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

查看更多
登录 后发表回答