ios UIButton 删除所有目标操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3340825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIButton remove all target-actions
提问by SK9
I have added multiple target-action-forControlEvents: to a UIButton. I'd like to remove all of these in one go without deallocating anything. I will then set new targets.
我添加了多个 target-action-forControlEvents: 到 UIButton。我想一次性删除所有这些而不释放任何东西。然后我会设定新的目标。
Is this possible and how do I go about it?
这是可能的,我该怎么做?
回答by progrmr
Call removeTarget:action:forControlEvents:, pass nil for the target, NULL for action, and use a control mask that sets all bits (UIControlEventAllEvents).
调用removeTarget:action:forControlEvents:,为目标传递 nil,为动作传递 NULL,并使用设置所有位的控制掩码 (UIControlEventAllEvents)。
Objective-C
目标-C
[someControl removeTarget:nil
action:NULL
forControlEvents:UIControlEventAllEvents];
Swift 2
斯威夫特 2
button.removeTarget(nil, action: nil, forControlEvents: .AllEvents)
Swift 3
斯威夫特 3
button.removeTarget(nil, action: nil, for: .allEvents)
回答by Hlung
@progrmr's answer in Swift 2:
@progrmr 在 Swift 2 中的回答:
button.removeTarget(nil, action: nil, forControlEvents: .AllEvents)
and Swift 3:
和斯威夫特 3:
button.removeTarget(nil, action: nil, for: .allEvents)
Note: Swift doesn't have NULL
, so I tested replacing it with nil
and it seems to work fine.
注意:Swift 没有NULL
,所以我测试用 替换它,nil
它似乎工作正常。
回答by Iya
Swift:
迅速:
btnCancel.removeTarget(self, action: Selector(), forControlEvents: UIControlEvents.AllEvents)
回答by Riajur Rahman
Swift 2:
斯威夫特 2:
actionButton.removeTarget(nil, action: nil, forControlEvents: .AllEvents)
Swift 3 & 4:
斯威夫特 3 和 4:
actionButton.removeTarget(nil, action: nil, for: .allEvents)
Objective-C:
目标-C:
[actionButton removeTarget: nil action: NULL forControlEvents: UIControlEventAllEvents];
Hope it helps.
希望能帮助到你。
回答by UdayM
- removeTarget:action:forControlEvents:
This method stops the delivery of events to the specified target object.
此方法停止向指定的目标对象传递事件。
Specifying a valid objectin the target parameter, this method stops the delivery of the specified events to all action methods associated with that object.
Specifying nilfor 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)
在目标参数中指定一个有效对象,此方法会停止将指定事件传递给与该对象关联的所有操作方法。
为目标参数指定nil,此方法可防止将这些事件传递给所有目标对象的所有操作方法
目标-c:
[_myButton removeTarget: //any validObject (or) nil action:nil forControlEvents:UIControlEventAllEvents];
迅速:
myButton.removeTarget(*validObject or nil*, action:nil, forControlEvents:UIControlEvents.AllEvents)