-->

[__NSCFType searchKeyword:]: unrecognized selector

2019-04-26 21:00发布

问题:

The following code is to add a subview to current view from storyboard:

EventSearchViewController* view1 = [self.storyboard instantiateViewControllerWithIdentifier:@"searchView"];

[view1 setBookingSystem:system];

[self.view addSubview:view1.view];

In the view "view1", there is a textField. The following is a IBAction to the textField and the event is "Did end on exit".

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

The following is the error message.

2012-05-26 20:26:47.369 OnlineBooking[6607:f803] -[__NSCFType searchKeyword:]: unrecognized selector sent to instance 0x6d8eb80

2012-05-26 20:26:47.369 OnlineBooking[6607:f803] * WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: -[__NSCFType searchKeyword:]: unrecognized selector sent to instance 0x6d8eb80

回答1:

You need to retain your EventSearchViewController, or keep a strong reference to it if you're using ARC. If you assign it to view1 as a local variable, it's not going to be around any more when searchKeyword: gets called. (The error shows that its memory has been released and re-used for a different type of object.)



回答2:

For me the problem was that I never called

- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

before I called

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

again.