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

发布于 2025-01-16 20:23:28 字数 262 浏览 0 评论 0原文

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

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

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黑色毁心梦 2025-01-23 20:23:28

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

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
}
怪我鬧 2025-01-23 20:23:28

jrturton 所说的这里,您可以将其变成一个小扩展:

extension UIButton {
    func removeAllActions() {
        enumerateEventHandlers { action, _, event, _ in
            if let action = action {
                removeAction(action, for: event)
            }
        }
    }
}

现在您只需在 UIButton 上调用 removeAllActions() 即可达到相同的效果。

Taking what jrturton has said here, you can turn it into a small extension:

extension UIButton {
    func removeAllActions() {
        enumerateEventHandlers { action, _, event, _ in
            if let action = action {
                removeAction(action, for: event)
            }
        }
    }
}

And now you can just call removeAllActions() on your UIButton to have the same effect.

跨年 2025-01-23 20:23:28

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文