I am using XCode 5 under ios 7 to do a simple project. And I get the following error when I press a button that is created programmatically:
2013-11-10 09:16:02.969 Project2[446:70b] +[KNSunDynamicUIButton buttonSavePressed:]: unrecognized selector sent to class 0x221424
Details:
I create a custom method that is used to create a UIButton object programmatically. That custom method is used in any view controller. That custom method needs passed-in parameters like x, y, width, height, button title, and selector that is an event handler and passed in like "(SEL)selector".
Custom method (belongs to a helper class):
+(UIButton*)kNSunSetupWithSelector:(SEL)selector withX:(int)x y:(int)y width:(int)width height:(int)height
buttonTitle:(NSString*)buttonTitle backGroundColor:(CGColorRef) backGroundColor
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Save" forState:UIControlStateNormal];
button.frame = CGRectMake(x, y, width, height);
button.backgroundColor = [UIColor greenColor];
// selector is used over here
[button addTarget:self
action:selector
forControlEvents:UIControlEventTouchDown];
return button;
}
And then in a view controller .m file, I call that custom method like:
-(void)setUpButtons
{
SEL selector = @selector(buttonSavePressed:);
_buttonSave = [KNSunDynamicUIButton kNSunSetupWithSelector:selector withX:470 y:410 width:160 height:40 buttonTitle:@"Save" backGroundColor:(_buttonSaveColor)];
[self.view addSubview:_buttonSave];
}
@interface MyViewController ()
{
...
// buttons
UIButton* _buttonSave;
}
@end
And the definition of button's event handler in the same view controller .m file like:
- (void)buttonSavePressed:(UIButton*)button
{
NSLog(@"Button Save clicked.");
}
When I run my code and hit over the button, I see the exception as mentioned above. Please help thank you.
P.S. If I rewrite the custom method as an alternative that does not have "(SEL)selector" parameter in its signature, and let controller view, who calls that custom method, does the job of coding selector, then there is no exception:
-(void)setUpButtons
{
//Note: the codes are in my view controller .m file
_buttonSave = [KNSunDynamicUIButton kNSunSetupWithX:470 y:410 width:160 height:40 buttonTitle:@"Save" backGroundColor:_buttonSaveColor];
// Note: selector coding is taken care by codes of my view controller instead of by custom method
[_buttonSave addTarget:self
action:@selector(buttonSavePressed:)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:_buttonSave];
[_buttonSave setupView];
}
And the alternative custom method (I do not like the method because it does not take care of dynamic passed-in selector):
+(UIButton*)kNSunSetupWithX:(int)x y:(int)y width:(int)width height:(int)height
buttonTitle:(NSString*)buttonTitle backGroundColor:(CGColorRef) backGroundColor
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Save" forState:UIControlStateNormal];
button.frame = CGRectMake(x, y, width, height);
button.backgroundColor = [UIColor greenColor];
return button;
}