Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Button programming one within the viewDidLoad method.

Hello! I did two implementations for this extra task. I would be very grateful if you can comment them for me, which one is better to use and why.

Here they are (one is active and other is commented):

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(29, 477, 261, 49)];

// UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; // btn.frame = CGRectMake(29, 477, 261, 49);

[btn setImage:[UIImage imageNamed:@"button-off"] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@"button-on"] forState:UIControlStateHighlighted];

1 Answer

I think there is no significant difference in two ways that you alloc and init UIButton. buttonWithType: is just a simple method that does the allocation for you.