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 alloc
ated 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
精彩评论