Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

iphone - UIButton setTitleColor:forState: question

Why does the following code work...

[signInBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[signInBtn setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];

while this does not?

[signInBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted|UIControlStateDisabled];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I know this is an old question, but these answers aren't correct.

When you set each separately you are saying the state property should be UIControlStateHighlighted OR UIControlStateDisabled but NOT both

When you bitwise or them together you are stating they must BOTH be set in the state property. Meaning UIControlStateHighlighted AND UIControlStateDisabled are set in the state property.

The example code below perfectly illustrates my point. If you disagree run it for yourself.

[button setTitle:@"highlighted and selected" forState:UIControlStateHighlighted | UIControlStateSelected];
[button setTitle:@"Highlighted only" forState:UIControlStateHighlighted];
[button setTitle:@"Selected only" forState:UIControlStateSelected];
[button setTitle:@"Normal" forState:UIControlStateNormal];

NSLog(@"Normal title: %@", [[button titleLabel] text]); // prints title: Normal

[button setSelected:YES];

NSLog(@"Selected title: %@", [[button titleLabel] text]); // prints title: Selected only 

[button setSelected:NO];
[button setHighlighted:YES];

NSLog(@"highlighted title: %@", [[button titleLabel] text]); // prints title: Highlighted only

[button setSelected:YES];

NSLog(@"highlighted and selected title: %@", [[button titleLabel] text]); // prints title: highlighted and selected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...