Given (arbitrarily):
CGRect frame = CGRectMake(0.0f, 0.0f, 100.0f, 30.0f);
What\'s the difference between the following two code snippets?<
The main (maybe the only) difference is in memory management: as you said, buttonWithType
returns an autoreleased UIButton
. This way you don't have to worry about releasing it. On the other hand, you don't own it, so you cannot release it when you want to (except, of course, if you drain
the autorelease pool).
Calling explicitly [[UIButton alloc] initWithFrame:frame]
, instead, you dynamically alloc your button, so you own it and you're responsible for releasing it.
If you plan to retain your button for some reason then maybe you should consider the second solution, but if, as in this case, you are autoreleasing it immediately, there's no big difference between the two way of creating a button...