简体   繁体   中英

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 imageNamed:@"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. Thanx in advance.

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 inital 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 propery 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

Happy Coding :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM