I need 2 different images for highlighted state for an UIButton.
I've these lines of code:
- (IBAction)buttonPressed:(id)sender
{
UIImage *followImageHighlighted = [UIImage imageNamed:@"follow-hilite.png"];
UIImage *unfollowImageHighlighted = [UIImage imageNamed:@"unfollow-hilite.png"];
if ([sender isSelected]) {
// set this image for the next time the button will pressed
[sender setImage:unfollowImageHighlighted forState:UIControlStateHighlighted];
} else {
// set this image for the next time the button will pressed
[sender setImage:followImageHighlighted forState:UIControlStateHighlighted];
}
}
- (void)viewDidLoad
{
// ...
UIImage *followImage = [UIImage imageNamed:@"follow.png"];
UIImage *unfollowImage = [UIImage imageNamed:@"unfollow.png"];
[self.followButton setImage:followImage forState:UIControlStateNormal];
[self.followButton setImage:unfollowImage forState:UIControlStateSelected];
}
The problem is that every time I press the button I see the highlighted image follow-hilite.png.
Can't I change the highlighted image for a button on the road?
I think this is a bad limitation because when the button is selected (thus, "Following") and an user press it, he see the default image, then when it touch up the image is that for selected state and when the network operation is completed then the button image switch correctly to the selected one.
Ideas?
EDIT
- (IBAction)followButtonTapped:(id)sender
{
BOOL isFollowed = [sender isSelected];
NSString *urlString = isFollowed ? kUnfollowURL : kFollowURL;
// operation [...]
[self.followButton setSelected:(isFollowed) ? NO : YES];
self.user.followed = !isFollowed;
}
I explain better the problem:
- button in default state: black text on white background
- button in selected state: white text on black background
If the target user is not followed, the button is in default state and if I try to press it I see the correct highlighted image.
BUT if the target user is followed and the button is in selected state, if I try to press it (and hold the finger) I see the button with black text on white background. This is very ugly and this is my problem.
[sender isSelected]is, but it's only returning yes. - CodaFi