简体   繁体   中英

Different images for highlighted UIButton state


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.

The IBAction is an awkward (at best, or impossible) place to configure the control. There must be some condition in your app that triggers the requirement for the different highlighted image. Configure the button when you detect that condition.

Use the "pressed" callback for taking whatever action the app is supposed to take on the press.

I've solved with:

[myButton setImage:imageSelectedHover forState:(UIControlStateSelected | UIControlStateHighlighted)];

Glad it works. You solved it by updating the application condition : self.user.followed. Now, to make it really right, try this:

- (IBAction)followButtonTapped:(id)sender
{
    NSString *urlString = self.user.followed? kUnfollowURL : kFollowURL;
    // operation [...]
    self.user.followed = !self.user.followed;
}

The state of your model is what matters here. The selected state of the button is more like a bool that was lying around where you are keeping a copy of the real following state.

I think you need to cast sender to UIButton* before you try to modify anything important and work out your variables, because sender does not include a method or property called -isSelected . Try this instead:

- (IBAction)buttonPressed:(id)sender
{
    UIImage *followImageHighlighted = [UIImage imageNamed:@"follow-hilite.png"];
    UIImage *unfollowImageHighlighted = [UIImage imageNamed:@"unfollow-hilite.png"];
    if ([self isSelected]) {
        // set this image for the next time the button will pressed
        [(UIButton*)sender setImage:unfollowImageHighlighted forState:UIControlStateHighlighted];
    } else {
        // set this image for the next time the button will pressed
        [(UIButton*)sender setImage:followImageHighlighted forState:UIControlStateHighlighted];
    }
[self isSelected] = ![self isSelected];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM