在IOS中单击时更改按钮文本和背景颜色

时间:2022-11-21 21:11:49

I know this question is asked frequently, I have applied many codes but still I'm not getting solution.

我知道这个问题经常被问到,我已经应用了很多代码,但我仍然没有得到解决方案。

I have three radio buttons, when I select radiobutton1 it shows show buttons, now i want that when the user click radiobutton1 it shows button, the first button is preselected and if the user wants to select other button from option the button background color and text color should change.

我有三个单选按钮,当我选择radiobutton1它显示show按钮时,现在我想要当用户点击radiobutton1它显示按钮时,第一个按钮被预先选择,如果用户想要从选项中选择其他按钮按钮背景颜色和文本颜色应该改变。

I'm trying some code but only first button color is changed it does not change other buttons colors. My code is:

我正在尝试一些代码,但只更改了第一个按钮颜色,它不会更改其他按钮颜色。我的代码是:

-(void)btnClick:(UIButton *)sender{

    switch (sender.tag) {
        case 1:
            if ([rd1 isSelected]==YES ) {

                [rd1 setSelected:YES];
            }
            else{
                [rd1 setSelected:YES];

                [rd2 setSelected:NO];

                [rd3 setSelected:NO];

                _house.hidden=NO;

                _flat.hidden=NO;

                _upperP.hidden=NO;

                _lowerP.hidden=NO;

                _farmH.hidden=NO;

                _roomB.hidden=NO;

                _pentH.hidden=NO;

                _resident.hidden=YES;

                _commercial.hidden=YES;

                _agricult.hidden=YES;


                _industry.hidden=YES;

                _plot.hidden=YES;

                _plotF.hidden=YES;

                _office.hidden=YES;

                _shop.hidden=YES;

                _wareH.hidden=YES;

                _factory.hidden=YES;

                _building.hidden=YES;

                _other.hidden=YES;

                if ([_house isSelected]==YES) {

                    House=_house.titleLabel.text;

                    NSLog(@"Button is %@",House);

                    _flat.enabled=NO;

                    _upperP.enabled=NO;

                    _lowerP.enabled=NO;

                    _farmH.enabled=NO;

                    _roomB.enabled=NO;

                    _pentH.enabled=NO;

                }

                else if ([_flat isSelected]==YES) {

                    _house.enabled=NO;

                    _upperP.enabled=NO;

                    _lowerP.enabled=NO;

                    _farmH.enabled=NO;

                    _roomB.enabled=NO;

                    _pentH.enabled=NO;


                }

                [self BgColor];


            }
            break;

        case 2:
            if ([rd2 isSelected]==YES ) {

                [rd2 setSelected:NO];
            }
            else{
                [rd2 setSelected:YES];

                [rd1 setSelected:NO];

                [rd3 setSelected:NO];

                _resident.hidden=NO;

                _commercial.hidden=NO;

                _agricult.hidden=NO;

                _industry.hidden=NO;

                _plot.hidden=NO;

                _plotF.hidden=NO;

                _house.hidden=YES;

                _flat.hidden=YES;

                _upperP.hidden=YES;

                _lowerP.hidden=YES;

                _farmH.hidden=YES;

                _roomB.hidden=YES;

                _pentH.hidden=YES;

                _shop.hidden=YES;

                _wareH.hidden=YES;

                _factory.hidden=YES;

                _building.hidden=YES;


                _other.hidden=YES;

                _office.hidden=YES;




            }
            break;

        case 3:
            if ([rd3 isSelected]==YES ) {

                [rd3 setSelected:NO];
            }
            else{
                [rd3 setSelected:YES];

                [rd2 setSelected:NO];

                [rd1 setSelected:NO];

                _office.hidden=NO;

                _shop.hidden=NO;

                _wareH.hidden=NO;

                _factory.hidden=NO;

                _building.hidden=NO;

                _other.hidden=NO;

                _house.hidden=YES;

                _flat.hidden=YES;

                _upperP.hidden=YES;

                _lowerP.hidden=YES;

                _farmH.hidden=YES;

                _roomB.hidden=YES;

                _pentH.hidden=YES;
                _resident.hidden=YES;

                _commercial.hidden=YES;

                _agricult.hidden=YES;

                _industry.hidden=YES;

                _plot.hidden=YES;

                _plotF.hidden=YES;

            }
            break;

        default:
            break;
    }

}

-(void)BgColor{

    if ([_house isSelected]==YES) {

        [_house setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        _house.backgroundColor=[UIColor redColor];

    }
    else if (([_flat isSelected]==YES)){

        [_flat setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        _flat.backgroundColor=[UIColor redColor];
    }
}

在IOS中单击时更改按钮文本和背景颜色

2 个解决方案

#1


1  

One possible problem could be that you call [self BgColor]; only inside the case 1: block. Since that method seems to take care of the title and background color for house and flat, maybe you should always call it (after the switch statement).

一个可能的问题可能是你打电话给[self BgColor];仅在案例1:块内。由于该方法似乎照顾了房屋和公寓的标题和背景颜色,也许你应该总是称之为(在switch语句之后)。

If that doesn't work, my advice is to break the problem in smaller pieces. For example, create 3 IBActions, one for each button. Give outlet names to each one of the buttons so that you know exactly what you're changing every time. Try to move repeating code to its own method. Test all the time and in small steps until you make it!

如果这不起作用,我的建议是以较小的碎片来解决问题。例如,创建3个IBActions,每个按钮一个。为每个按钮指定插座名称,以便您准确了解每次更改的内容。尝试将重复代码移动到自己的方法。一直进行测试,然后逐步进行测试,直至完成!

To get you started, here's a sample project that has some basic similar functionality.

为了帮助您入门,这是一个具有一些基本类似功能的示例项目。

#2


0  

I think for radio button, you just need to set Image for all for. So you just have to make a button to selected/normal state.

我认为对于单选按钮,你只需要为所有人设置图像。所以你只需要按一个按钮选择/正常状态。

You can set image for all different state like this:

您可以为所有不同的状态设置图像,如下所示:

[YOUR_BUTTON setImage:YOUR_IMAGE_NORMAL forState:UIControlStateSelected];
[YOUR_BUTTON setImage:YOUR_IMAGE_SELECTED forState:UIControlStateNormal];
[YOUR_BUTTON setImage:YOUR_IMAGE_HEIGHLIGHED forState:UIControlStateHighlighted];
[YOUR_BUTTON setImage:YOUR_IMAGE_DISABLE forState:UIControlStateDisabled];

About the text color, let me give you one small trick for it. Check my below method:

关于文本颜色,让我给你一个小技巧。检查我的以下方法:

-(void)btnClick:(UIButton *)sender{
     // This is button on which user is clicked
    UIButton *btnCurrent = (UIButton *)sender;

    if(btnReference != nil)
    {
        // If this is not a nil, it means you have clicked another button previously.
        // So make it normal before doing any thing.
        btnReference.selected = false;
        [btnReference setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        // Assign reference of current button
        btnReference = btnCurrent;
    }

    // Make changes for clicked button
    btnReference.selected = TRUE;
    [btnReference setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [btnReference setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

}

Put this condition on top of your method. So you don't have to worry about color and all.

将此条件置于您的方法之上。所以你不必担心颜色和所有。

#1


1  

One possible problem could be that you call [self BgColor]; only inside the case 1: block. Since that method seems to take care of the title and background color for house and flat, maybe you should always call it (after the switch statement).

一个可能的问题可能是你打电话给[self BgColor];仅在案例1:块内。由于该方法似乎照顾了房屋和公寓的标题和背景颜色,也许你应该总是称之为(在switch语句之后)。

If that doesn't work, my advice is to break the problem in smaller pieces. For example, create 3 IBActions, one for each button. Give outlet names to each one of the buttons so that you know exactly what you're changing every time. Try to move repeating code to its own method. Test all the time and in small steps until you make it!

如果这不起作用,我的建议是以较小的碎片来解决问题。例如,创建3个IBActions,每个按钮一个。为每个按钮指定插座名称,以便您准确了解每次更改的内容。尝试将重复代码移动到自己的方法。一直进行测试,然后逐步进行测试,直至完成!

To get you started, here's a sample project that has some basic similar functionality.

为了帮助您入门,这是一个具有一些基本类似功能的示例项目。

#2


0  

I think for radio button, you just need to set Image for all for. So you just have to make a button to selected/normal state.

我认为对于单选按钮,你只需要为所有人设置图像。所以你只需要按一个按钮选择/正常状态。

You can set image for all different state like this:

您可以为所有不同的状态设置图像,如下所示:

[YOUR_BUTTON setImage:YOUR_IMAGE_NORMAL forState:UIControlStateSelected];
[YOUR_BUTTON setImage:YOUR_IMAGE_SELECTED forState:UIControlStateNormal];
[YOUR_BUTTON setImage:YOUR_IMAGE_HEIGHLIGHED forState:UIControlStateHighlighted];
[YOUR_BUTTON setImage:YOUR_IMAGE_DISABLE forState:UIControlStateDisabled];

About the text color, let me give you one small trick for it. Check my below method:

关于文本颜色,让我给你一个小技巧。检查我的以下方法:

-(void)btnClick:(UIButton *)sender{
     // This is button on which user is clicked
    UIButton *btnCurrent = (UIButton *)sender;

    if(btnReference != nil)
    {
        // If this is not a nil, it means you have clicked another button previously.
        // So make it normal before doing any thing.
        btnReference.selected = false;
        [btnReference setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        // Assign reference of current button
        btnReference = btnCurrent;
    }

    // Make changes for clicked button
    btnReference.selected = TRUE;
    [btnReference setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [btnReference setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

}

Put this condition on top of your method. So you don't have to worry about color and all.

将此条件置于您的方法之上。所以你不必担心颜色和所有。