开发者

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

Pretty much says it all... I've added a couple of targets to my UIButton, I just need to know if I need to remove them before the UIButton is released (and dealloc'd), or is it okay just to assume it'll tidy itself up?

Thanks!

EDIT: The scenario is this: A UIViewController which creates a button and has targets set as self:

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

So now the button is being owned by the superview, and autoreleased at the end of the run loop. So, if I later remove the button from the view, will it dealloc okay, or do I also need to do removeTarget:action:forControlEvents: in order for the button to go away? I'm guessing the former, since I hop开发者_如何学Ce targets are assigned and not retained, but it'd be nice for someone to confirm it please? :)


Do you mean you have other objects which are using the UIButton as a target?

If so - if those objects were implemented correctly - they should have their member variable getter/setter property set to "retain" - which means they should hold a reference to your UIButton.

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

That's of course if I'm understating your question correctly...


You have to remove the targets and cleanup your self.

Assume that you have an object O that has a button B and that V adds itself as target of B. Assume also that O exposes B somehow and that there is someone else retaining B. When you release O, B will still be alive and will point to O. Triggering any action on B will cause a crash.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜