3

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?

Anastasia
  • 3,024
  • 1
  • 23
  • 34
  • This seems like the completely wrong way of going about trying to make a custom action for a button. Are you experienced at all with Objective-C programming? – Dan F Apr 17 '13 at 13:57
  • @DanF The goal is to use this button in different xibs without copypasting the action, that will be the same for all cases. Could you suggest another approach to do this? – Anastasia Apr 17 '13 at 14:07

3 Answers3

4

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.

tkanzakic
  • 5,499
  • 16
  • 34
  • 41
  • Thanks. Still can not understand why it doesn't work with `addTarget:nil`, but at least it works with `addTarget:self`. – Anastasia Apr 17 '13 at 15:20
  • when you call `addTarget:action:forControlEvents:` you are telling the compiler that, when the event specified in the last parameter is produced, it have to call the selector defined in action of the object specified on the Target parameter, if you specify `nil` then the method is send to `nil` – tkanzakic Apr 17 '13 at 15:26
  • I thought, that `addTarget:nil` means the same, if I connect the action to the first responder in IB, and it means that system should use the responder chain to dispatch the action. (I learned it from here: http://stackoverflow.com/questions/598455/objective-c-cocoa-first-responder-did-i-get-that-right ) If this is correct, than I would expect that my button's method will be called as first responder's method. And I can't understand why it does'n work as expected. – Anastasia Apr 17 '13 at 15:41
  • yes, that is how it works from IB, but from the code it is not the same, from the code you have to specify the target and the action – tkanzakic Apr 17 '13 at 17:07
3

Depending on what you are doing with the button - there is no real need to make a subclass.

Here is some code how to make a simple button that triggers a method:

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setFrame:CGRectMake(10, 10, 100, 30)];
[myButton setTitle:@"Title" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];

the add target bit tells the button to trigger the given selector when the user touches up inside the button (taps it). The target is the object that the selector is in.

Hope this helps, you should not need to subclass UIButton unless you have custom layouts or specific custom functionality for it.

fatuous.logic
  • 750
  • 1
  • 5
  • 16
2

Since you want to handle the action from the button code itself, you may have to override this method - (void)sendActionsForControlEvents:(UIControlEvents)controlEvents and handle your event as if it was sent to your button;

sixthcent
  • 1,150
  • 7
  • 6