Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Simple iPhone App (iOS7) Designing your App Customizing a Button

About buttons views labels etc.

Hi i hope you're having a great night!, i recently did the homework asked in this particular crystalball application about erasing the button from the storyboard and creating it programmatically. This is the code that i have done:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *buttonOff = [UIImage imageNamed:@"button-off"];
    UIImage *buttonOn = [UIImage imageNamed:@"button-on"];
    UIButton *predictButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [predictButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
    [predictButton setBackgroundImage:buttonOff forState:UIControlStateNormal];
    [predictButton setBackgroundImage:buttonOn forState:UIControlStateHighlighted];
    predictButton.frame = CGRectMake(85.0, 470.0, 160.0, 40.0);
    UIImage * backgroundImage = [UIImage imageNamed:@"background"];
    UIImageView * imageView = [[UIImageView alloc] initWithImage:backgroundImage];
    [self.view insertSubview:imageView atIndex:0];
    [self.view insertSubview:predictButton aboveSubview:imageView];
self.crystalBall = [[CrystalBall alloc] init];
}

It seems to work pretty nice, but my question is, how should i separate the button specs? do i should create a custombuttonclass with the methods? or just put some methods in viewcontroller and call them at viewdidload? what is the propper way to separate and handle my views code? for labels buttons etc. Hope you can help me!

2 Answers

Buttons are one of those things that you should generally "never" subclass. Instead, you should "always" use decorator methods such as what you have done to change its appearances and behaviors. So what you've done is the correct way to go.

Wow thanks John, so, for example if i have to customize a lot of buttons saying that i have more views with buttons should i go like what i did before? but this just no fill the viewController.m with a lot of code if i have more views to modify or customize??! Thanks for the answer!