iPhone:无法更改按钮背景
我遇到一种情况,我正在尝试更改 4 个按钮的背景图像,如下所示:
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];
}
但是当我触摸任何一个按钮时,其他四个按钮的背景就会消失 请告诉我如何解决这个问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您正在寻找单选按钮,您可以为单选按钮编写一个自定义类,并使事情轻松解决。
这段代码实际上在每个控制状态下都能正常工作。我认为问题在于按钮的初始图像和当前按钮状态(笔尖和代码中)。
按钮的状态可以通过以下方式更改:
当您专门为控件状态设置图像时,图像上的更改将出现在该特定控件状态上。因此,更改取决于初始图像和按钮的当前控制状态
注意:我们正在背景图像上应用更改。按钮有一个图像属性,实际上可以隐藏背景图像的变化。如果您在代码中的任何位置设置图像
,也许它可以隐藏您的背景图像设置。
此外,您的 if-else 循环中也可能会出现错误...遍历循环并验证每个条件是否图像设置正确。
May be you are looking for the radiobutton, You can write down a custom class for radio button and make things work out easily.
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:
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
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.