Spike Lee
Spike Lee

Reputation: 411

IBAction button change color?

I have created a button in interface builder and set its type as custom. I want to change the background color of the button based on the button's tag when the view loads. How can i do this?

Upvotes: 1

Views: 1167

Answers (1)

bryanmac
bryanmac

Reputation: 39296

Button added to Xib, tag set to 1.

View header:

@property (nonatomic, retain) IBOutlet UIButton *myButton;

In interface builder, bound myButton to button.

Then programmatically in viewDidLoad, conditionally set based on tag value:

- (void)viewDidLoad
{
    [super viewDidLoad];

    if ([myButton tag] == 1)
    {
        [myButton setBackgroundColor:[UIColor blueColor]];
    }
    else
    {
        [myButton setBackgroundColor:[UIColor greenColor]];
    }
}

Upvotes: 1

Related Questions