0
votes

In a UIButton subclass:

- (void) setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];

    if (highlighted) {
        self.backgroundColor = [UIColor redColor];
    }
    else {
        self.backgroundColor = [UIColor blueColor];
    }
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    if (selected) {
        self.backgroundColor = [UIColor greenColor]; //this happens
    }
    else {
        self.backgroundColor = [UIColor blueColor];
    }
}

I see the red color when the button is highlighted. But when I set button.selected = YES, I never see a green color. Why does setHighlighted work but setSelected doesn't?

I can set a breakpoint on the "this happens" line, and it happens. But the background color of the button doesn't change to green.

1
What attributes do you have set in interface builder?Jared
there are a couple of answers out there. stackoverflow.com/questions/4049103/… it looks like one common solution is to set a background image that is basically a 1x1 pixel of a color. This solution works because you set the background image for each specific state. I'm not really sure why your code doesnt work. does the button get unselected right after it gets selected?mitrenegade

1 Answers

0
votes

Are you setting the selected property inside your button's UIControlEventTouchUpInside action? If so, your setHighlighted: method is getting called again after setSelected:, so backgroundColor is being set back to blue.