Switch selectedState between three UIButton on the same tab

I have 3 UIButton on the same tab.

The problem : When I click on button1, button2 and button3 all of them got selected; however what I want is when I select (for example) button2, the other buttons be unselected (like radio buttons on html). Any idea on how I can solve this issue ?

Here is my code :

- (IBAction)button1:(UIButton *)sender {
    sender.selected = !sender.selected;
 }
- (IBAction)button2:(UIButton *)sender {
 sender.selected = !sender.selected;
 }
- (IBAction)button3:(UIButton *)sender {
 sender.selected = !sender.selected;
 }

Answers


There are a couple of straightforward solutions. Maybe they are not optimal, but you can use them:

First solution:

Link your buttons with corresponding properties:

@property (weak, nonatomic) IBOutlet UIButton *button1;
@property (weak, nonatomic) IBOutlet UIButton *button2;
@property (weak, nonatomic) IBOutlet UIButton *button3;

In each action change the selected property for each button:

- (IBAction)button1Clicked:(UIButton *)sender {
    _button1.selected = YES;
    _button2.selected = NO;
    _button3.selected = NO;
}

Second solution:

Add all your buttons in NSArray:

NSArray *buttonsArray = [[NSArray alloc] initWithObjects: button1, button2, button3, nil];

Then link all the buttons with one action:

- (IBAction)buttonClicked:(UIButton *)sender {
    for (UIButton *btn in _buttonsArray) {
        if (btn == sender)
            btn.selected = YES;
        else
            btn.selected = NO;
    }
}

Don't forget to make your NSArray accessible for all the methods of the class, for example by creating a corresponding property:

@property (weak, nonatomic) NSArray *buttonsArray;

Third solution:

You can change the tag property for each button (through Interface Builder or in code):

_button1.tag = 1;
_button2.tag = 2;
_button3.tag = 3;

Then link all the buttons with one action (just like in previous example):

- (IBAction)buttonClicked:(UIButton *)sender {
    if (sender.tag == 1) {
        _button1.selected = YES;
        _button2.selected = NO;
        _button3.selected = NO;
    } else if (sender.tag == 2) {
        _button1.selected = NO;
        _button2.selected = YES;
        _button3.selected = NO;
    } else if (sender.tag == 3) {
        _button1.selected = NO;
        _button2.selected = NO;
        _button3.selected = YES;
    } 
}

Fourth solution: (by @velmurugan-s)

Use one action for all the buttons and set their default state to selected = NO:

- (IBAction)buttonClicked:(UIButton *)sender {
    _button1.selected = NO;
    _button2.selected = NO;
    _button3.selected = NO;

    sender.selected = YES;
}

Summary:

I'd recommend you to use the second solution as the most optimal one.


What you can do is connect button1,button2,button3 to same button123 in interface section give them appropriate tags

- (IBAction)button123:(UIButton *)sender;

In its description use below code

- (IBAction)button123:(UIButton *)sender {

if (sender.tag == 111)
{
    sender.selected = !sender.selected;

    if (sender.selected)
    {
        self.button2.selected = NO;
        self.button3.selected = NO;


    }

}
else if (sender.tag == 222)
{
    sender.selected = !sender.selected;

    if (sender.selected)
    {
       self.button1.selected = NO;
        self.button3.selected = NO;

    }


}
else if (sender.tag == 333)
{
    sender.selected = !sender.selected;

    if (sender.selected)
    {
      self.button1.selected = NO;
        self.button2.selected = NO;

    }

}

- (IBAction)button1:(UIButton *)sender {
    btn1.selected = YES;
    btn2.selected = NO;
    btn3.selected = NO;

 }
- (IBAction)button2:(UIButton *)sender {

    btn1.selected = NO;
    btn2.selected = YES;
    btn3.selected = NO;
 }
- (IBAction)button3:(UIButton *)sender {
    btn1.selected = NO;
    btn2.selected = NO;
    btn3.selected = YES;
 }

You had change the selected sate for a button that you have selected,You need to change all other button state too.


Need Your Help

How to preserve GTK look & feel in Java using sudo

java linux gtk environment-variables sudo

Often when you need to run some Java installers, you just do something like this:

Errno 2 using python shutil.py No such file or directory for file destination

python xml linux file shutil

I am using the shutil python module to copy files and directories on a linux redhat machine.