开发者

UIButton - should we release or not?

开发者 https://www.devze.com 2022-12-18 15:01 出处:网络
I want to add a button on a view dynamically. Here is the code. UIButton *x=[UIButton buttonWithType:UIButtonTypeRoundedRect];

I want to add a button on a view dynamically. Here is the code.

UIButton *x=[UIButton buttonWithType:UIButtonTypeRoundedRect];

Here, I have not used "alloc" function.

The questions for this statements are as follow.

  • If we are using imageview for the same situation, we have to create an temp imageview add to your current view and release it. What to do for button?
  • How are buttons allocated and de-allocated?
  • If buttons are allocated? How it's memory is been mana开发者_Python百科ged?
  • Now when I use [x release]; - is it right or wrong?


No alloc/init or new so it will be autoreleased when it is no longer needed. When you add it to the UIView the count is increase and retained by the view, then also released when the view is released.


You do not need to call release in this case. Since you are using a convenience constructor, the object that is returned is an autoreleased object.
If you use an alloc / init form, you are responsible for releasing the object.


UIButton *x=[UIButton buttonWithType:UIButtonTypeRoundedRect];

using this method u cant relese the button

instead of use

UIButton *x = [[UIButton alloc]init];

and then release

0

精彩评论

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

关注公众号