0xSina
0xSina

Reputation: 21553

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 stuck on this for a while so any help would be appreciated...Thanks!

Upvotes: 0

Views: 1341

Answers (1)

Warren Burton
Warren Burton

Reputation: 17372

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.

Upvotes: 1

Related Questions