If you are changing the title of a UIButton programmatically, you will naturally use the method setTitle:forState:
. Easy enough… well, sort of. What do you put in the state parameter?
My first reaction was “I’m going to put all possible states since I just want the button title to change no matter what.” So I blindly bit-OR‘ed all the possible values defined in the docs.
[myButton setTitle:@"New Title"
forState:UIControlStateNormal | UIControlStateHighlighted | UIControlStateDisabled | UIControlStateSelected];
Sound reasonable right? Wrong. If you do that here, your button title is probably not going to change at all. The problem is that the “normal” state (per Apple docs that’s “enabled, not selected, not highlighted”) is defined as such:
enum {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0,
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2,
...
}
so if you bit-OR all of them you are actually going to set the title for ALL states EXCEPT the “normal” (most common) state!
Sigh…