15

I am using UIImageView's with UIButtons a whole bunch. So, I created a custom class to permanently marry these two an make things a little simpler. It all works well until I decided to implement -(id)initWithObject:(AUIImageViewButton *) imageViewButton.

Clearly I need to copy all relevant properties from the imageViewButton object being passed. The UIImageView is not problematic at all. Something like this deals with it:

imageview = [[UIImageView alloc] initWithFrame:imageViewButton.imageview.frame];        // Copy all relevant data from the source's imageview
[imagebutton.imageview setBackgroundColor:imageViewButton.imageview.backgroundColor];   //
[imagebutton.imageview setImage:imageViewButton.imageview.image];                       //

Most of the button stuff is also readily available:

button = [UIButton buttonWithType:imageViewButton.button.buttonType];                   // Copy all relevant data from the source's button
button.frame = imageViewButton.imageview.frame;                                         // 
[button setTitle:imageViewButton.button.titleLabel.text forState:UIControlStateNormal]; //
button.tag = imageViewButton.button.tag;                                                //

I am having a little trouble figuring out how to get all the data for the addTarget:action:forControlEvents method.

Looking at the docs I can see that I might be able to use UIControl's allControlEvents and allTargets methods. I'll dig into that right now and see how much trouble I can get into. The one I am not sure about is the action.

Can anyone give me a shove in the right direction?

Thanks,

-Martin

martin's
  • 3,853
  • 6
  • 32
  • 58
  • Just checking, you do know that UIButton supports both a background image (where the title text shows on top) and a image (where no title text shows)? What features of UIImageView do you need? – Bogatyr Mar 03 '11 at 16:13

3 Answers3

38

UIControl's allTargets and allControlEvents are the way to start. The final piece of the puzzle is actionsForTarget:forControlEvent:, call it once for each target and event.

Anomie
  • 92,546
  • 13
  • 126
  • 145
21

Showing how to iterate over a button's targets and create copies of the selector on another button. Specific example is just the touchupinside event, but that's usually all I use.

for (id target in button.allTargets) {
     NSArray *actions = [button actionsForTarget:target
                                      forControlEvent:UIControlEventTouchUpInside];
     for (NSString *action in actions) {
          [newButton addTarget:target action:NSSelectorFromString(action) forControlEvents:UIControlEventTouchUpInside];
     }
}
Joel Teply
  • 3,260
  • 1
  • 31
  • 21
1

I used this to remove any possible unwanted target/action before assigning a new one:

if let action = button.actions(forTarget: target, forControlEvent: .touchUpInside)?.first
{
    button.removeTarget(target, action: NSSelectorFromString(action), for: .touchUpInside)
}

or if you really want to remove all actions:

if let actions = button.actions(forTarget: target, forControlEvent: .touchUpInside)
{
    for action in actions
    {
        button.removeTarget(target, action: NSSelectorFromString(action), for: .touchUpInside)
    }
}
zero3nna
  • 2,770
  • 30
  • 28