Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different image when button from storyboard tapped

I have a button that I want to use a different background image when in highlighted state, normally I would use something like

[buttonObject setBackgroundImage:[UIImage imageNamed:@"buttonDown.png"] 
                        forState:UIControlStateHighlighted];

But the button is in a .storyboard file, not code.
What should I use in this situation?

like image 474
Peter V Avatar asked Aug 27 '12 22:08

Peter V


2 Answers

You can set up different properties for different states in IB too, by selecting the state from the drop down menu:

enter image description here

If you want to do it in code, you have to set up an outlet and set the images in awakeFromNib.

@property (nonatomic, weak) IBOutlet UIButton *button;

To connect the outlet, you Ctrl-drag from the button to your File Owner (probably a view controller) and select the outlet defined above.

Then you can access the button in your code:

- (void)awakeFromNib {
    [button setBackgroundImage:[UIImage imageNamed:@"buttonDown.png"] 
                      forState:UIControlStateHighlighted];
}
like image 185
DrummerB Avatar answered Sep 18 '22 14:09

DrummerB


Why not using outlets?

Configuring the View from Apple doc.

Hope that helps.

like image 35
Lorenzo B Avatar answered Sep 18 '22 14:09

Lorenzo B