iphone: unable to change background of buttons
I have a situation I'm trying to change background image of 4 buttons like this:
if(some condition){
[firstSeverityButton setBackgroundImage:[UIImage imageNamed:@"greySeverity.jpg"] forState:UIControlStateDisabled];
[secondSeverityButton setBackgroundImage:[UIImage imageNamed:@"greySeverity.jpg"] forState:UIControlStateNormal];
[thirdSeverityButton setBackgroundImage:[UIImage imageName开发者_StackOverflow社区d:@"greySeverity.jpg"] forState:UIControlStateNormal];
[fourthSeverityButton setBackgroundImage:[UIImage imageNamed:@"greySeverity.jpg"] forState:UIControlStateNormal];
[fifthSeverityButton setBackgroundImage:[UIImage imageNamed:@"redSeverity.png"] forState:UIControlStateNormal];
}
But the background of the other four disappears when I touch any one Please enlighten me on this how can his be tackled.
May be you are looking for the radiobutton, You can write down a custom class for radio button and make things work out easily.
[firstSeverityButton setBackgroundImage:[UIImage imageNamed:@"greySeverity.jpg"] forState:UIControlStateDisabled];
This code actually works fine, with every control state. I think the problem is with the initial image for the button and the current button state (in both nib and code).
The state of buttons can be changed with:
button.enabled=yes;
button.highlighted = NO;
button.selected = NO;
When you setup image for control state specifically, the change on image appear on that specific control state. So the change depends on both the initial image and the current control state of button
NOTE: We are applying change on the background image. There is an image property for the button which can actually hide the changes of background image. If you setup the image
[firstSeverityButton setImage:[UIImage imageNamed:@"greySeverity.jpg"] forState:UIControlStateDisabled];
anywhere in code and maybe it can hide your background image setting.
Also there is a possibility of error in your if-else loop ...go through the loops and verify every conditions whether the images get set properly.
精彩评论