有没有办法从 UIControl 中删除所有 UIAction?

Is there a way to remove all UIActions from a UIControl?

或者有没有办法检查 UIControl 是否有任何 UIAction?

有时我想更改 UIControl 的行为。 使用目标动作时,我可以这样做

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

你为什么不使用 view.isUserInteractionEnabled = false?它将移除视图的所有交互能力

您可以使用 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
}