Do I need to remove targets from UIButton before I release it?

Quite a lot says this ... I have added several goals to my UIButton, I just need to know if I need to remove them before UIButton is released (and dealloc'd), or is it okay to just assume that it will remove itself ?

Thanks!

EDIT: the scenario is: A UIViewController that creates a button and has goals set to self :

 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:btn]; 

So, now the button belongs to the super-verb and is auto-implemented at the end of the launch cycle. So, if I later remove the button from the view, will it dealloc be fine, or do I also need to do removeTarget:action:forControlEvents: so that the button leaves? I guess the first one, since I hope that the goals are set and not saved, but would it be nice if someone confirms this, please? :)

+4
source share
2 answers

Do you mean that you have other objects that use UIButton as the target?

If this is the case - if these objects were implemented correctly - they must have their getter / setter attribute of their members so that it is "held", which means that they must contain a link to your UIButton.

Therefore, you can free your UIButton, but if the reference objects still reference it, they will retain the UIButton.

This, of course, if I understand your question correctly ...

+1
source

You need to remove goals and clean yourself.

Suppose you have an object O that has a button B and that V adds itself as target B. Suppose also that O somehow opens B and that there is someone else saving B. When you release O , B will still be alive and point to O. Running any action on B will crash.

Surely my example is contrived, but it's better to be safer than sorry.

-2
source

All Articles