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 unti
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];
}
}
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)
}
}
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.