文章目录
  1. 1. UIButton target-actions
  2. 2. UIButton remove all target-actions

UIButton target-actions

Ios基础语法 Ios-UIButton Ios有用代码

由于ios里的UIButton的事件如果要动态加入事件的话,通过调用其方法- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
但这个方法有个问题,就是它会不断的往这个button里添加事件,哪怕是相同的事件,这样的话点击这个button就会触发多个事件同时运行,那如果我只想让一个事件运行,而且是动态添加的事件呢?

这里我整了很久,官方没找到好的方式,那只能通过野路子了,我的思路是这样的,可不可以在new 一个button的时候给action弄一个坑,占个位,以后的事件要新加的话只要往坑里填东西就可以了,于是我弄了20分钟,最后发现太麻烦了,我得重新那个坑的方法。有没有更简单的方法?

最后我想,反正我每次添加的时候之前的事件我都不用了,那我直接删除就不好了吗?
嗯,我又干了20分钟,如果在每个页面删除指定事件的话,太繁锁了,而且要指定target和action,官方又没有提供删除所以绑定的action的方法。
最后只能找谷姐了,一搜在stackOverflow发现用这种方式可以解除所有绑定:

UIButton remove all target-actions

|up vote85down votefavorite22

|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.

Is this possible and how do I go about it?

iphone uibutton
|share|improve this question
|asked Jul 27 ‘10 at 4:50Jul 27 ‘10 at 4:50
-156.png

SK9

6,8917747479292

|
|—————————————————————————————————————————————————|————————————————————————————————————————————————————————————-|

|
|—————————————————————————————————————————————————————————————————————————————————————————————————————————|——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————–|
| | |

## 1 Answer

activeoldestvotes

[|up vote224down voteacceptedaccepted
|Call removeTarget:action:forControlEvents: pass nil for the target, NULL for action, and use a control mask that sets all bits (UIControlEventAllEvents). Something like this:

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

|share|improve this answer
|
|———————————————————————————————————————————————–|

|
|———————————————————————————————–|—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————-|

真爽啊,话说这个也困了我一天,周未没事弄了下给解决了]()

文章目录
  1. 1. UIButton target-actions
  2. 2. UIButton remove all target-actions