1
- (void)updateFollowButton {
  if (!followButton.selected) {
    [followButton setImage: [UIImage imageNamed:kFollowButtonImage] forState:UIControlStateNormal];
    [followButton setImage:[UIImage imageNamed:kButtonFollowHighlighted] forState:UIControlStateHighlighted];
    [followButton setBackgroundImage:[UIImage imageNamed:kFollowButtonImage] forState:UIControlStateDisabled];
  } else {
    [followButton setImage:[UIImage imageNamed:kUnfollowButtonImage] forState:UIControlStateNormal];
    [followButton setImage:[UIImage imageNamed:kButtonfUnfollowHighlighted] forState:UIControlStateHighlighted];
    [followButton setImage:[UIImage imageNamed:kUnfollowButtonImage] forState:UIControlStateDisabled];
  }
}

当我设置选择状态时,我调用 updateFollowButton 来刷新按钮正常图像并突出显示图像。为什么这个简单的代码不起作用?

感谢您的回答。

4

1 回答 1

0

您应该使用选定的状态。按位或您的状态UIControlStateSelected来设置您拥有的 3 个不同状态的值,但何时selected==YES

然后,状态会自动处理(这意味着一旦您配置了所有状态,您就不必再次调用您的方法来更改图像,就像您对您的 所做的那样updateFollowButton):

- (void)configureFollowButton
{
   // Call this method only once (in your viewDidLoad method of your ViewController, typically) to configure your button once and for all
   [followButton setImage: [UIImage imageNamed:kFollowButtonImage] forState:UIControlStateNormal];
   [followButton setImage:[UIImage imageNamed:kButtonFollowHighlighted] forState:UIControlStateHighlighted];
   [followButton setBackgroundImage:[UIImage imageNamed:kFollowButtonImage] forState:UIControlStateDisabled];
   [followButton setImage:[UIImage imageNamed:kUnfollowButtonImage] forState:UIControlStateNormal|UIControlStateSelected];
   [followButton setImage:[UIImage imageNamed:kButtonUnfollowHighlighted] forState:UIControlStateHighlighted|UIControlStateSelected];
   [followButton setImage:[UIImage imageNamed:kUnfollowButtonImage] forState:UIControlStateDisabled|UIControlStateSelected];
}

-(IBAction)followButtonAction:(UIButton*)sender
{
   sender.selected = !sender.selected;
   [self doFollowAction:sender.selected];
   // And no need to call "updateFollowButton" there: the button image is already updated to reflect the UIControlStateSelected state when you change its selected property.
}
于 2012-10-25T17:17:22.950 回答