Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In theory you could use a conditional like <code>if ([label.backgroundColor isEqual:[UIColor blackColor]) { ... }</code>, but you have to be careful because two colors that look the same may not necessarily pass <code>isEqual:</code>. (e.g. if one is grayscale and the other is 24 bit RGB).</p> <p>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. </p> <p>For example, let's say your label's color represents a weapon selection in a rock, paper, scissors game:</p> <pre><code>// .h typedef enum WeaponState { WeaponStateRock, WeaponStateScissors, WeaponStatePaper } WeaponState; // .m - (IBAction)weaponSelectorButtonClicked { if (weaponState == WeaponStateRock) { weaponState = WeaponStateScissors; } else if (weaponState == WeaponStateScissors) { weaponState = WeaponStatePaper; } else { weaponState = WeaponStateRock; } [self refreshView]; } - (void)refreshView { if (weaponState == WeaponStateRock) { self.label.backgroundColor = [UIColor redColor]; } else if (weaponState == WeaponStateScissors) { self.label.backgroundColor = [UIColor blackColor]; } else { self.label.backgroundColor = [UIColor blueColor]; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload