Weird problem with UIControlStateHighlighted
I am running into a weird problem with UIButton. I have set it's background image for state UIControlStateHighlighted but the background image doesn't change I click on it. The target/selector still get called, BUT, if I remove the target/selector, then it works just fine.
Here's my code:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(currentXPos, 0, minWidth + additionalSpacing, self.bounds.size.height)];
[button setBackgroundImage:[UIImage imageNamed:@"cellBackgroud.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"cellBackgroudSelected.png"] forState:UIControlStateHighlighted];
[button setTitle:text forState:UIControlStateNormal];
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[[button titleLabel] setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14]];
[button setTag:i];
[button addTarget:delegate action:@selector(topCellPressed:) forControlEvents:UIControlStateHighlighted];
[self addSubview:button];
I have been st开发者_如何学运维uck on this for a while so any help would be appreciated...Thanks!
You are using the wrong type for your target designation. You want a a UIControlEvent
not a UIControlState
. The thing you are setting your button up for currently is garbage as control state is a very different bit mask to control event.
Nominally for a button press you want UIControlEventTouchUpInside
[button addTarget:delegate action:@selector(topCellPressed:) forControlEvents:UIControlEventTouchUpInside];
Im surprised the compiler hasnt given you a warning about this.
精彩评论