开发者

what is the difference between this two lines?

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

and

UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

What is the difference between these two declarat开发者_JAVA技巧ions or are both the same?


The first one will assign a UIButton object to btn. You are responsible for releasing it when you are finished, since you allocated the memory.

The second one will perform the same action, but the object will be autoreleased, meaning that you do not have to call release explicitly, as the operating system will perform that action when necessary.

Note: The UIButtonType is also different.


[UIButton buttonWithType:...]

creates an autoreleased object (which still needs memory).

[[UIButton alloc]init] 

creates an object which is not going to be autoreleased. you have to release by yourself!

have a further look at this question.

And more about memory management.


First one gives you a not autoreleased UIButton with a buttonType of UIButtonTypeCustom

Second one gives you an autoreleased UIButton with a buttonType of UIButtonTypeRoundedRect

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜