Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UIButton Target, Action and Control events?

Tags:

iphone

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

like image 480
martin's Avatar asked Mar 03 '11 15:03

martin's


People also ask

How do I add a target to UIButton?

Using a UIControl with a closure callback viewDidLoad() let button = UIButton(type: . system) button. addTarget(self, action: #selector(buttonTapped(_:)), for: . touchUpInside) } @IBAction func buttonTapped(_ sender: UIButton) { print("Button tapped!") } }

How do I delete all targets UIButton?

Call removeTarget:action:forControlEvents:, pass nil for the target, NULL for action, and use a control mask that sets all bits (UIControlEventAllEvents). Aside: the -allTargets instance method returns an NSSet of all the instance's targets (nil if none).

What happens when I set the target of a UIControl to nil?

Here is description of parameter Target from apple's documentation for UIControl class: target The target object—that is, the object to which the action message is sent. If this is nil, the responder chain is searched for an object willing to respond to the action message.


1 Answers

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.

like image 129
Anomie Avatar answered Oct 06 '22 23:10

Anomie