objective-c 当用户在 iPhone 上按下该按钮时更改 UIButton 上的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1072698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:50:55  来源:igfitidea点击:

Changing image on UIButton when user presses that button on an iPhone

iphoneobjective-c

提问by Nicsoft

I want to change the image on UIButton, when User presses that button. So, I wrote

当用户按下该按钮时,我想更改 UIButton 上的图像。所以,我写了

btnthumbnail2 setImage:[UIImage imageNamed:@"leaderboard_ov.png"] forState:UIControlStateNormal];

and after that I changed the view.

之后我改变了看法。

The image is changing but not display the changed image.I think the controll goes to next view tha's why it is happening. But I want to show the change, How can I do this?? Plz help me for this..

图像正在更改但不显示更改后的图像。我认为控件会转到下一个视图,这就是它发生的原因。但我想显示变化,我该怎么做??请帮我这个..

回答by Nicsoft

I am using Interface Builder, in which I added a UIButtonto the view.

我正在使用 Interface Builder,在其中我向UIButton视图添加了 a 。

  1. I defined the button selectedby default and selected my image for the selectedstate (Use drop-down list in Inspector window to choose "Selected State Configuration" before selecting the image).

  2. Create an IBActionin the controller and connect the button to that action.

  3. Then see the code below:

  1. 我定义的按钮选择默认的选择和我的形象选定状态(使用下拉列表中的Inspector窗口选择“选中状态配置选择前映像”)。

  2. IBAction在控制器中创建一个并将按钮连接到该操作。

  3. 然后看下面的代码:

  -(IBAction) toggleUIButtonImage:(id)sender{
      if ([sender isSelected]) {
         [sender setImage:unselectedImage forState:UIControlStateNormal];
         [sender setSelected:NO];
      } else {
         [sender setImage:selectedImage forState:UIControlStateSelected];
         [sender setSelected:YES];
      }
  } 

I think this is quite a smooth solution. Hope it helps!

我认为这是一个非常顺利的解决方案。希望能帮助到你!

回答by Anjaan

This is the simplest way that I am doing it and it works every time

这是我正在做的最简单的方法,它每次都有效

-(void)buttonTouched:(id)sender
{
 UIButton *btn = (UIButton *)sender;

 if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"icon-Locked.png"]])
    {
       [btn setImage:[UIImage imageNamed:@"icon-Unlocked.png"] forState:UIControlStateNormal];
       // other statements
    }
 else
   {
       [btn setImage:[UIImage imageNamed:@"icon-Locked.png"] forState:UIControlStateNormal];
       // other statements
   }
}

You can use other similar checks for other states such as SELECTED, ENABLED, HIGHLIGHTED.

您可以对其他状态使用其他类似检查,例如 SELECTED、ENABLED、HIGHLIGHTED。

回答by zpasternack

The code you have there should work fine, but I'm guessing that the button image doesn't appear changed until the next run through the event loop, so you don't see it before the view gets switched out.

您在那里的代码应该可以正常工作,但我猜按钮图像在下一次运行事件循环之前不会出现更改,因此在视图切换之前您看不到它。

It's kind of a hack, but you could try delaying the view change just slightly so that the button will update.

这是一种黑客行为,但您可以尝试稍微延迟视图更改,以便按钮更新。

So instead of this:

所以而不是这个:

[btnthumbnail2 setImage:[UIImage imageNamed:@"leaderboard_ov.png"] forState:UIControlStateNormal];
[self showMyOtherView];

Try this:

尝试这个:

[btnthumbnail2 setImage:[UIImage imageNamed:@"leaderboard_ov.png"] forState:UIControlStateNormal];
[self performSelector:@selector(showMyOtherView) withObject:self afterDelay:0.01];

回答by Ramin

To change the image for the selected state you have to call setImagefor state UIControlStateSelected. You can set separate images for different states (Normal, Highlighted, Disabled, Selected, etc).

要更改所选状态的图像,您必须调用setImagestate UIControlStateSelected。您可以为不同的状态(正常、突出显示、禁用、选定等)设置单独的图像。

回答by user3554689

If you want to change the image when the button is clicked, this way, works perfectly:

如果您想在单击按钮时更改图像,这种方式非常有效:

-(IBAction)buttonClicked: (id)sender
{   
  UIButton *buttonObj = (UIButton*)sender;
  [buttonObj setBackgroundImage:[UIImage imageNamed:@"myImage.png"]   
  forState:UIControlStateNormal];
}

回答by superhawk610

The simplest way to accomplish this is to simply set the highlighted image in Interface Builder.

完成此操作的最简单方法是在 Interface Builder 中简单地设置突出显示的图像。

回答by Danra

Building on Nicsoft's answer.

基于 Nicsoft 的回答。

-(void)viewDidLoad {
    [super viewDidLoad];
    [self.button setImage:[UIImage imageNamed:@"unselectedImageName"] forState:UIControlStateNormal];
    [self.button setImage:[UIImage imageNamed:@"selectedImageName"] forState:UIControlStateSelected];
}

-(IBAction)toggleButton:(UIButton *)sender {
    sender.selected = !sender.selected;
    // Add logic here
}

回答by mmc

I wrote code that does exactly this last night, and I didn't have to resort to any strange delaying tactics. In this particular case, I'm doing it from inside a tableViewCell. This is more code than you asked for, but it shows the whole process I used. As you can see, I chose not to have any special image for the period of time DURING a button touch.

我昨晚写的代码正是这样做的,而且我不必诉诸任何奇怪的延迟策略。在这种特殊情况下,我是从 tableViewCell 内部进行的。这是比您要求的更多的代码,但它显示了我使用的整个过程。如您所见,我选择在触摸按钮期间不显示任何特殊图像。

// Button set up, inside cellForRowAtIndexPath:
// imageFrame is just the rect that you want the button to occupy
  [self setFavorite:[[UIButton alloc] initWithFrame:imageFrame]];
  [[self favorite] addTarget:self action:@selector(toggleFavorite:)
        forControlEvents:UIControlEventTouchUpInside];
  [cell addSubview:[self favorite]];
  [self draw];


// definition of the callback specified above
- (void) toggleFavorite:(id) sender {
  if (favoriteState == 0 ){
    favoriteState = 1;
  } else {
    favoriteState = 0;
  }
  [self draw];
}

// the draw method, to set the images
// favOn and favOff are statically defined at the top of the class
- (void) draw {
  if (favoriteState != 0) {
    [[self favorite] setBackgroundImage:favOn forState:UIControlStateNormal];
    [[self favorite] setBackgroundImage:favOn forState:UIControlStateHighlighted];
  } else {
    [[self favorite] setBackgroundImage:favOff forState:UIControlStateNormal];
    [[self favorite] setBackgroundImage:favOff forState:UIControlStateHighlighted];
  }
}

回答by Tom Howard

Hate to add to the plethora of answers, but:

讨厌添加过多的答案,但是:

  1. Select images in IB for the button's default and selected states.
  2. Connect an IBAction and keep it simple.

    @IBAction func buttonTapped(sender: UIButton){sender.selected = !sender.selected}

  1. 在 IB 中为按钮的默认和选定状态选择图像。
  2. 连接一个 IBAction 并保持简单。

    @IBAction func buttonTapped(sender: UIButton){sender.selected = !sender.selected}

Done.

完毕。