Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setImage forState method sets the image for all the states on a button

I have this small problem that i cannot figure out. I have about 16 buttons and they are connected as an outlet collection to my controller.And also they have an action method which makes the buttons change their state from default to selected. Im trying to change all the buttons' images for only default state. So i run a loop through the array and set their image for the default state using setImage: forState: method. However the method changes the images for all states( Default and selected states ).

This is the setter method for my outlet collection

- (void) setCardsButton:(NSArray *)cardsButton
{
    _cardsButton = cardsButton;

    for (UIButton *button in cardsButton) {

         [button setImage:[UIImage imageNamed:@"card.png"] forState:UIControlStateNormal];

    }
     [self updateView];

}
like image 286
fozkul Avatar asked Jun 23 '13 20:06

fozkul


2 Answers

If you don't specify an image for another state, the image for the 'normal' (UIControlStateNormal) state will be used instead. So, explicitly set the image you want to be used for state UIControlStateSelected.

If the 'normal' state isn't set then the system default is used.

like image 74
Wain Avatar answered Oct 01 '22 15:10

Wain


Any properties you set for the "Normal" state are used for all other states not explicitly set otherwise. This is stated in the docs for UIButton setImage:forState:. If you want a different image for other states, you need to call setImage:forState: for the other states as well.

like image 43
rmaddy Avatar answered Oct 01 '22 17:10

rmaddy