gpt4 book ai didi

ios - 是否可以在 Swift 中覆盖 UIButton 的操作方法?

转载 作者:搜寻专家 更新时间:2023-10-30 21:51:59 25 4
gpt4 key购买 nike

我有一个 UIButton。我想使用相同的 UIButton 来执行多个操作。首先,我以编程方式将操作设置为按钮。

button1.addTarget(self, action: #selector(ViewController.function1), forControlEvents: .TouchUpInside)

接下来,我想放弃那个功能并想添加其他操作。

button1.addTarget(self, action: #selector(ViewController.function2), forControlEvents: .TouchUpInside)

是否可以覆盖按钮的现有目标?

最佳答案

您建议的情况不会覆盖上一个操作,而是将第二个操作添加到按钮,导致 ViewController.function1ViewController.function2 被调用。

在使用添加新操作之前,您需要从目标中删除之前的操作

button1.removeTarget(self, action: #selector(ViewController.function1), forControlEvents: .AllEvents)

或者在添加新 Action 之前删除所有以前的 Action

button1.removeTarget(nil, action: nil, forControlEvents: .AllEvents)

关于ios - 是否可以在 Swift 中覆盖 UIButton 的操作方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38186670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com