0

UIButton Action 方法在执行时不显示任何结果。基本上 UIButton 默认显示 Bookmark-N 图像,当 UIButton 被按下时它应该用 Bookmark-YES 图像替换 Bookmark-N。

按下 UIButton 时,它并没有替换图像,实际上没有做任何事情。

@property (weak, nonatomic) IBOutlet UIButton *bookmarkbtn;

- (IBAction)bookmarkAction:(id)sender;


- (IBAction)bookmarkAction:(id)sender {

if ( _bookmarkbtn.tag)

{
    [_bookmarkbtn setImage:[UIImage imageNamed:@"Bookmark-Y.png"] forState:UIControlStateNormal];
}
else 
{
    [_bookmarkbtn setImage:[UIImage imageNamed:@"Bookmark-N.png"] forState:UIControlStateNormal];
}
}

我认为这与这句话有关

if ( _bookmarkbtn.tag)

因为这个语句如果是真的只有那么它会执行rest。

任何想法请如何解决此声明。

4

2 回答 2

2

First set button selected image and normal images.

        [_bookmarkbtn setImage:[UIImage imageNamed:@"Bookmark-N.png"]
           forState:UIControlStateNormal];
        [_bookmarkbtn setImage:[UIImage imageNamed:@"Bookmark-Y.png"]
           forState:UIControlStateSelected];

and change the status of the button to selected when button pressed. Check sender tag with your _bookmarkbtn tag.

  - (IBAction)bookmarkAction:(id)sender {

       if ( _bookmarkbtn.tag == sender.tag)

      {
         _bookmarkbtn.selected = !_bookmarkbtn.selected;
     }

}
于 2013-08-03T18:55:05.280 回答
1

示例代码:

- (IBAction)bookmarkAction:(id)sender 
{
    if ( _bookmarkbtn.tag)
    {
          [_bookmarkbtn setImage:[UIImage imageNamed:@"Bookmark-Y.png"] forState:UIControlStateNormal];
          _bookmarkbtn.tag = 0;
    }
    else 
    {
          [_bookmarkbtn setImage:[UIImage imageNamed:@"Bookmark-N.png"] forState:UIControlStateNormal];
          _bookmarkbtn.tag = 1;
    }
}
于 2013-08-03T19:08:03.700 回答