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 に答える