【问题标题】:How do I change a button's color when pressed and reset to original color when a different button is pressed?如何在按下时更改按钮的颜色并在按下不同按钮时重置为原始颜色?
【发布时间】:2014-07-09 11:48:38
【问题描述】:

所以我有两个灰色的UIButton,button1 和 button2。当我按下另一个按钮时,我想将按钮的颜色更改为红色并返回灰色。

我使用了这个代码:

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

但这会使两个按钮都变红。

如果button1是红色的,我怎么做,当我按下button2时它会变回灰色?

【问题讨论】:

    标签: ios uibutton


    【解决方案1】:

    创建两个按钮 btn1 & btn2 。

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

    btn1 和 btn2 都应该连接到 btnClick 。下面的代码将完成您的其余工作

    - (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]];
    
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      您将要制作两个单独的按钮。从您的代码看,您只声明了一个按钮。

      在界面中声明两个按钮,在 ViewDidLoad() 方法中初始化它们。

      为每个按钮设置 2 个不同的目标,这样当单击一个按钮时,您可以设置其他按钮的颜色。

      【讨论】:

        【解决方案3】:

        我从中得到的是两个按钮都连接到相同的操作/方法,其中包含您提供的代码。执行您指定的操作的最简单方法是使用两种不同的操作/方法,并将每个按钮连接到不同的方法。将 button1 连接到 button1Pressed 并将 button2 连接到 button2Pressed。

        在 .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;
        

        在 .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]];
        }
        

        希望这会有所帮助!

        【讨论】:

          【解决方案4】:

          有很多方法可以解决这个问题,在你的情况下,我们真的不知道你是如何实例化你的UIButton。以下目的是您使用标签的可能解决方案之一:

          首先你应该在标题中定义你的按钮:

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

          然后,你应该在你的视图中实现它们,并给它们附加 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];
          }
          

          最后要做的是实现你的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];
              }
          }
          

          正如我之前提到的,有很多方法可以执行这种算法,但我喜欢这个,因为我们对你的后台应用程序了解不多,而且这个很容易为你定制。

          【讨论】:

            猜你喜欢
            • 2019-10-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-07-16
            • 1970-01-01
            • 2012-07-05
            相关资源
            最近更新 更多