Take the 2-minute tour ×

Given (arbitrarily):

CGRect frame = CGRectMake(0.0f, 0.0f, 100.0f, 30.0f);

What's the difference between the following two code snippets?


    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;


    UIButton *button = [[[UIButton alloc] initWithFrame:frame] autorelease];
share|improve this question
add comment

5 Answers

I think they're equivalent. Haha! Trick question you sneaky little punk!


  1. -buttonWithType: returns an autoreleased UIButton object.

  2. +[NSObject alloc] defaults scalar instance variables to 0, so buttonType should be 0, or UIButtonTypeCustom.

Pros & Cons

  1. You could argue that it's clearer to use -buttonWithType: and set buttonType explicitly and that it's safer in case Apple changes UIButtonTypeCustom to be 1 instead of 0 (which will most certainly never happen).

  2. On the other hand, you could also argue that it's clear & safe enough to use -initWithFrame. Plus, many of the Xcode sample projects, such as "TheElements" & "BubbleLevel," use this approach. One advantage is that you can explicitly release the UIButton before the run loop for your application's main thread has drained its autorelease pool. And, that's why I prefer option 2.

share|improve this answer
+1 for internal dialog. –  glenstorey Aug 22 '12 at 17:54
"-[NSObject init] defaults scalar instance variables to 0" No, it's actually +alloc that sets instance variables to 0. "so buttonType should be 0" No, this depends on what -[UIButton init] (if there is such a method) does. Sure, it might currently set it to 0 or doesn't set it (in which case it would be 0); but since that method it not documented, they could change its behavior in the future. You have no guarantees on what it does. –  user102008 Aug 27 '12 at 21:06
I know this is old but I have noticed you have to use [UIButton buttonWithType:] are your button type never gets set. Also you've written -buttonWithType as an instance method it should be a class method so you need to replace - with +. –  Popeye Mar 28 '13 at 15:57
add comment

I would strongly suggest using the first approach (+buttonWithType), because that's the only way to specify the button type.

If you +alloc and -initWithFrame:, the buttonType is set to some standard value (not sure which, and this could change in later versions of the SDK) and you can't change the type afterwards because the buttonType property is read only.

share|improve this answer
add comment

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...

share|improve this answer
add comment

Two options are the same but I prefer option 2 because it can handle memory management

share|improve this answer
add comment

You can use the first and do "[button retain];", so you will never lose the pointer.

share|improve this answer
add comment

Your Answer


