简体   繁体   English

如何在按下按钮时更改按钮的颜色,并在按下其他按钮时重置为原始颜色?

[英]How do I change a button's color when pressed and reset to original color when a different button is pressed?

So I have two gray UIButton , button1 and button2. 所以我有两个灰色的UIButton ,button1和button2。 I want to change the color of the button when I press it to red and back to gray when I press the other button. 我想将按钮的颜色更改为红色,而按其他按钮时更改为灰色。

I used this code: 我使用以下代码:

UIButton *btn = (UIButton *)sender;
[btn setBackgroundColor:[UIColor redColor]];

But it makes both buttons red. 但这会使两个按钮都变成红色。

How do I make it so that if button1 is red, it will change back to gray when I press button2? 如何使按钮1为红色,而按按钮2时又变回灰色?

You are going to want to make two separate buttons. 您将要制作两个单独的按钮。 By the look of your code you only declared only one button. 从代码的外观来看,您仅声明了一个按钮。

Declare both buttons in your interface, initialize them in the ViewDidLoad() method. 声明界面中的两个按钮,然后在ViewDidLoad()方法中对其进行初始化。

Set 2 different targets for each of the buttons so when one is clicked you can set the other buttons colour. 为每个按钮设置2个不同的目标,因此当单击一个按钮时,可以设置其他按钮的颜色。

What I am getting from this is that both buttons are connected to the same action/method, which contains the code you gave. 我从中得到的是两个按钮都连接到相同的操作/方法,其中包含您提供的代码。 The easiest way to do what you specified is to have two different actions/methods, and connect each button to a different method. 执行指定操作的最简单方法是使用两种不同的操作/方法,并将每个按钮连接到不同的方法。 Connect button1 to button1Pressed and connect button2 to button2Pressed. 将button1连接到button1按下,并将button2连接到button2Pressed。

In .h put: 在.h中输入:

{
    //connect these buttons up either via interface builder or through code
    IBOutlet UIButton* button1;
    IBOutlet UIButton* button2;
}

- (IBAction)button1Pressed:(id)sender;
- (IBAction)button2Pressed:(id)sender;

In .m put: 在.m中输入:

//connect this method to "touch up inside" on button1
- (IBAction)button1Pressed:(id)sender
{
    //makes button1 red
    [button1 setBackgroundColor:[UIColor redColor]];
}

//connect this method to "touch up inside" on button2
- (IBAction)button2Pressed:(id)sender
{
    //changes button1 back to gray
    [button1 setBackgroundColor:[UIColor grayColor]];
}

Hope this helps! 希望这可以帮助!

There is many way to solve such issue, in your case we do not really know how you are instantiating your UIButton . 有很多方法可以解决此类问题,在您的情况下,我们并不真正知道如何实例化UIButton The following purpose you one of the possible solution using tag: 以下目的是您使用标记的一种可能的解决方案:

First you should define your button in your header: 首先,您应该在标题中定义按钮:

@implementation _4644518ViewController {
    UIButton *btn1;
    UIButton *btn2;
}

Then, you should implement them in your view and attach to them the tapped method: 然后,您应该在自己的视图中实现它们并将tapped方法附加到它们:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    btn1 = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 100, 30)];
    [btn1 setTag:101];
    [btn1 setTitle:@"btn1" forState:UIControlStateNormal];
    btn1.backgroundColor = [UIColor lightGrayColor];
    [btn1 addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn1];

    btn2 = [[UIButton alloc] initWithFrame:CGRectMake(5, 40, 100, 30)];
    [btn2 setTag:102];
    [btn2 setTitle:@"btn2" forState:UIControlStateNormal];
    btn2.backgroundColor = [UIColor lightGrayColor];
    [btn2 addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn2];
}

The last thing to do is to implement your btnTapped method: 最后要做的是实现您的btnTapped方法:

- (void)btnTapped:(id)sender
{
    if ([sender tag] == 101) {
        btn1.backgroundColor = [UIColor redColor];
        btn2.backgroundColor = [UIColor lightGrayColor];
    } else if([sender tag] == 102) {
        btn1.backgroundColor = [UIColor lightGrayColor];
        btn2.backgroundColor = [UIColor redColor];
    }
}

As I mentioned earlier, there is many way to perform such algorithm, but I like this one since we don't know many things about your background application and this one will be easy to custom for you. 正如我之前提到的,执行这种算法的方法有很多,但是我喜欢这一方法,因为我们对您的后台应用程序了解不多,而且这种方法很容易为您定制。

Create two buttons btn1 & btn2 . 创建两个按钮btn1和btn2。

@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
- (IBAction)btnClick:(id)sender;

Both btn1 & btn2 should be connected to btnClick . btn1和btn2都应连接到btnClick Below code will do the rest of your work 下面的代码将完成您的其余工作

- (IBAction)btnClick:(id)sender {

    if (self.btn1 == sender) {

        [self.btn1 setBackgroundColor:[UIColor redColor]];
        [self.btn2 setBackgroundColor:[UIColor grayColor]];

    }else{

        [self.btn1 setBackgroundColor:[UIColor grayColor]];
        [self.btn2 setBackgroundColor:[UIColor redColor]];

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按下下一个按钮时如何更改上一个按钮的颜色? - How to Change previous button color when pressed the next button? 按下时更改以编程方式创建的按钮的颜色 - Change Color of Programmatically Created Button when Pressed 当在UIActivityViewController上单击“取消”按钮时,如何更改NavigationController的文本颜色? - How to change NavigationController text color when Cancel button is pressed on UIActivityViewController? 使用TouchableHighlight在React Native中按下时如何更改按钮颜色? - How to change button color when pressed in React Native using TouchableHighlight? 使用NSUSerDefaults如何在两次按下按钮时更改颜色 - How when Button pressed twice the color should change using NSUSerDefaults 按下按钮时更改自定义UIButton(CoreGraphics)的背景颜色 - change background color of custom UIButton (CoreGraphics) when button is pressed 按下按钮时如何更改图像? - How do I change an Image when a button is pressed? swift 按下按钮时如何切换背景颜色 - swift how to switch background color when button is pressed 将按钮按下到主机应用程序时如何更改自定义键盘的背景颜色? - How to change background color of custom keyboard when button is pressed into host app? 按下时如何使 UIView 改变颜色? - How to make UIView change color when pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM