views:

71

answers:

2

HI ALL,

I have created two custom buttons using IB and i have set their background image.Now i want that when a user selects a button its background image should change and the new background image should persists until user presses the other button.how to do this?

+2  A: 

You have to manage the states of your buttons by yourself in this case.
Meaning that you should hold a BOOL member for each button that will state if the button is selected.
Or, if you should have only one selected button in a time, then you might hold a reference to the selected button.
In the tap events you should manage the states above by changing the image of the last selected button to non-selected image and the image of the currently selected button to selected button image.

You can change the image like this:

[button setBackgroundImage:[UIImage imageNamed:@"selected_button.png"] forState:UIControlStateNormal];
Michael Kessler
A: 

You can combine 2 methods to do so:

- (void)setImage:(UIImage *)image forState:(UIControlState)state

call it using [self.button setImage:YOUR_IMAGE forState:UIControlStateSelected];

Then you can set the button to be selected. [self.button setState:UIControlStateSelected] . When another button is selected, you set the state back to normal.

vodkhang