Custom UIButton's action does not called

cocoa-touch, ios, iphone, objective-c, uibutton

Solution

If you want that your button always call the method declared add the code above to the `awakeFromNib` method

[self addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

and later when you added it to a view controller do not assign it a new action, this way it will always call your method.

Problem

I have subclass of `UIButton`: ``` @interface MyButton : UIButton - (IBAction) buttonPressed:(id)sender; @end @implementation MyButton - (void)awakeFromNib { NSLog(@"awake from nib"); } - (IBAction) buttonPressed:(id)sender { NSLog(@"button pressed"); } @end ``` I add a button in my parent xib, set it's class to MyButton, and connected it's action to `First Responder`'s method `buttonPressed`. When I start an application and load my parent xib with my button inside, than `awakeFromNib` from MyButton class is called. But when I press the button, nothing happens. I was expecting that my method `buttonPressed` from `MyButton` class will be called. I supposed, that my button's view is the first responder in responder chain, but apparently I do something wrong. Could you please suggest something?

Original source

Related problems