xcode 单击时如何更改当前图像的按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7765939/
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-14 22:07:32  来源:igfitidea点击:

How change the current image's button on Click?

objective-cxcodeimagebutton

提问by Joe

In my file .h :

在我的文件 .h 中:

-(IBAction)Boutton:(id)sender;

In my file .m :

在我的文件 .m 中:

-(IBAction)Boutton:(id)sender
{ 

    UIImage *btnImage1 = [UIImage imageNamed:@"x.png"];
    [sender setImage:btnImage1 forState:UIControlStateNormal];

}

With this code i can change the image of my clicked button (sender).

使用此代码,我可以更改单击的按钮(发件人)的图像。

The question is, how can I change the images of the others buttons (not the sender one)?

问题是,如何更改其他按钮(不是发件人按钮)的图像

For example if (sender.tag == 4)i would like To have something like :

例如,如果(sender.tag == 4)我想要像这样的东西:

-(IBAction)Boutton:(id)sender
{ 

    UIImage *btnImage1 = [UIImage imageNamed:@"x.png"];
    [sender setImage:btnImage1 forState:UIControlStateNormal];

    UIImage *btnImage2 = [UIImage imageNamed:@"Y.png"];

    [Boutton:(1) setImage:btnImage2 forState:UIControlStateNormal];
    [Boutton:(2) setImage:btnImage2 forState:UIControlStateNormal];
    [Boutton:(3) setImage:btnImage2 forState:UIControlStateNormal];

}

采纳答案by Jean-Denis Muys

Simply link the other buttons to outlets of your UIViewController subclass. So that from -(IBAction)Boutton:(id)senderyou can change their image by accessing them through their properties.

只需将其他按钮链接到 UIViewController 子类的出口。这样-(IBAction)Boutton:(id)sender您就可以通过它们的属性访问它们来更改它们的图像。

回答by Can ürek

The question is, how can I change the images of the others buttons (not the sender one) ?

问题是,如何更改其他按钮(不是发件人按钮)的图像?

Use tag parameters of UIButton.

使用 UIButton 的标签参数。

UIButton *btn = (UIButton *)sender;

if(btn.tag == 1)
{
...
}

回答by Diaa Den

choose the button and assign the image you want then from the utilities bar change the state config to Highlighted then assign the on click image

选择按钮并分配您想要的图像,然后从实用程序栏中将状态配置更改为突出显示,然后分配单击图像

see the screen shot below

看下面的屏幕截图

screen shot

截屏