Home > database >  Is there a way to remove all UIActions from a UIControl?
Is there a way to remove all UIActions from a UIControl?

Time:03-26

or is there a way to check whether a UIControl has any UIActions?

Sometimes I want to change a UIControl's behavior. When using target-action, I can do it like this

button.removeTarget(nil, action: nil), for: .touchUpInside)
button.addTarget(self, action: #selector(didTouchUpInside()), for: .for: .touchUpInside)

CodePudding user response:

Why not you are using view.isUserInteractionEnabled = false? It will remove all the interaction ability of a view

CodePudding user response:

You can do this using enumerateEventHandlers:

button.enumerateEventHandlers { action, targetAction, event, stop in
    if let action = action {
        // This is a UIAction
        button.removeAction(action, for: event)
    }
    if let (target, selector) = targetAction {
        // This is target / action
        button.removeTarget(target, action: selector, for: event)
    }
    stop = true // Ends iterating early if you are done
}
  • Related