根据iPhone开发中当前标签颜色更改标签颜色
我无法根据标签的当前颜色找出用于更改 UI 标签颜色的 if 语句代码。例如,如果标签颜色当前为红色并且按下了正确的按钮,我希望标签颜色更改为黑色。如果标签颜色为黑色,我希望标签颜色更改为蓝色。
I am having trouble figuring out the if statement code for changing the color of a UI label based on the current color of the label. For instance, if the label color is currently red and the correct button is pressed, I want the label color to change to black. If the label color is black I want the label color to change to blue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理论上,您可以使用像
if ([label.backgroundColor isEqual:[UIColor blackColor]) { ... }
这样的条件,但您必须小心,因为看起来相同的两种颜色不一定通过 isEqual: 。 (例如,如果一个是灰度,另一个是 24 位 RGB)。基于视图的视觉属性执行条件逻辑似乎是一个您可能需要重新考虑的设计决策。您将使将来更改视觉设计(例如,使用不同的颜色)而不在应用程序逻辑中引入错误变得更加困难。模型视图控制器模式鼓励我们解耦模型的状态、修改模型的逻辑以及直观地表示模型的视图。当标签在三种颜色之间循环时,颜色可能代表在三个可能值之间循环的某种底层逻辑状态。更好的选择是在基础模型中循环该状态,并让标签的颜色反映该状态。
例如,假设您的标签颜色代表剪刀石头布游戏中的武器选择:
In theory you could use a conditional like
if ([label.backgroundColor isEqual:[UIColor blackColor]) { ... }
, but you have to be careful because two colors that look the same may not necessarily passisEqual:
. (e.g. if one is grayscale and the other is 24 bit RGB).Executing conditional logic based on visual attributes of your view seems like a design decision that you may want to reconsider anyway. You're making it more difficult to change your visual design in the future (for example, to use different colors) without introducing bugs in your application's logic. The Model View Controller pattern encourages us to decouple the state of our model, the logic that modifies it, and views that represent it visually. When your label cycles between three colors, the color presumably represents some underlying logical state that cycles between three possible values. A better choice would be to cycle that state within your underlying model, and let the label's color reflect that state.
For example, let's say your label's color represents a weapon selection in a rock, paper, scissors game: