UIButton删除所有目标操作

358

我已经给一个UIButton添加了多个目标-动作事件。我想一次性删除所有这些目标-动作事件,而不需要释放任何东西。然后我会设置新的目标。

这是否可能?如何操作?

6个回答

871

调用 removeTarget:action:forControlEvents: 方法,将目标(target)参数传递为nil,动作(action)参数传递为NULL,并使用控件掩码(UIControlEventAllEvents)设置所有位。

Objective-C

[someControl removeTarget:nil 
                   action:NULL 
         forControlEvents:UIControlEventAllEvents];

Swift 2

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

Swift 3或更高版本

button.removeTarget(nil, action: nil, for: .allEvents)

4
谢谢你的提示!我认为这是完整的链接(即到部分):“http://developer.apple.com/iphone/library/documentation/uikit/reference/UIControl_Class/Reference/Reference.html#//apple_ref/occ/instm/UIControl/removeTarget:action:forControlEvents:" - Ken
1
progrmr的建议当然是有效的。为了补充答案,这里有一个像我需要的代码片段:[button removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside]; - Ken
6
附注:-allTargets实例方法返回一个NSSet,其中包含所有实例的目标(如果没有则为nil)。 - Ken
3
完美!正是我所期待的:D - Totumus Maximus
1
已更新为Swift 2和3,因为有竞争的答案完全相同,只是语言不同。 - Peter DeWeese
1
链接已更改为:https://developer.apple.com/documentation/uikit/uicontrol/1618248-removetarget - Panayotis

97

@progrmr在Swift 2中的回答:

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

以及Swift 3:

button.removeTarget(nil, action: nil, for: .allEvents)

注意:Swift没有NULL,因此我尝试将其替换为nil,似乎可以正常工作。


5
针对 Swift 3 版本,".AllEvents" 现在应该改为用小写字母 'a' 的 ".allEvents": removeTarget(nil, action: nil, for: .allEvents) - Sasho
关于你的注释:实际上,我相信在Objective-C中,你可以将nilNULL传递给第一个和第二个参数,并且它们都能正常工作。我相信两者都被定义为(void*) 0(或至少被视为相等)。 - Nicolas Miari
@Sasho 我很想看到一些统计数据,了解升级到Swift 3后源文件大小平均减少了多少(由于新的方法/参数命名规则)。 - Nicolas Miari
由于这是相同的答案,只是使用不同的语言,而且该问题中没有标记语言,因此应该对@progrmr的答案进行编辑。 - Peter DeWeese

9

Swift 3、4、5:

btnCancel.removeTarget(nil, action: nil, forControlEvents: UIControlEvents.AllEvents)

我可以翻译以下与编程相关的内容。请注明您需要翻译成哪种语言。 - Juan Boero
@JuanPabloBoero,这是 Swift。 - Iya

8

Swift 2:

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

Swift 3和4:

actionButton.removeTarget(nil, action: nil, for: .allEvents)

Objective-C:

[actionButton removeTarget: nil action: NULL forControlEvents: UIControlEventAllEvents];

4
- removeTarget:action:forControlEvents:

这个方法停止将事件传递到指定的目标对象。

  1. Specifying a valid object in the target parameter, this method stops the delivery of the specified events to all action methods associated with that object.

  2. Specifying nil for the target parameter, this method prevents the delivery of those events to all action methods of all target objects

    objective-c:

    [_myButton removeTarget:  //any validObject (or) nil
                  action:nil
        forControlEvents:UIControlEventAllEvents]; 
    

    swift:

    myButton.removeTarget(*validObject or nil*, action:nil, forControlEvents:UIControlEvents.AllEvents)
    

更多细节请参考 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIControl_Class/index.html#//apple_ref/occ/instm/UIControl/removeTarget:action:forControlEvents


0

如果条件允许,您可以更改选择器。 请参见下面的示例

您可以先删除所有目标,然后选择选择器并添加它。

rateButton.removeTarget(nil, action: nil, for: .allEvents)

    let action = interview.isRated ? #selector(viewTapped(_:)) : #selector(rateTapped(_:))
            
    rateButton.addTarget(self, action: action, for: .touchUpInside)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接