0

I've got a UIButton that acts as a switch. When user taps on it, its state changes to "selected" and action "one" is called. When user taps again UIButton state changes to "not selected" and the action is no longer available.

Is there a way to set the UIButton to "selected" by taping on a completely different UIButton and have it change to "selected" and call the same action as well?

Cheers

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
George Asda
  • 2,119
  • 2
  • 28
  • 54

6 Answers6

1

From what I have understood from the question, I would like to give you my suggestion

if (!<your_button>.selected)
{
    <your_button>.selected = YES;
    // do your stuff here
}
else
{
    <your_button>.selected = NO;
    // do your stuff here, in your case action should be no longer available so that,
    <your_button>.userInteractionEnabled = NO;
}

Enjoy Programming !

Niru Mukund Shah
  • 4,637
  • 2
  • 20
  • 34
0

simple thing when you tap on the uibutton make an action for that and in the action for that button , you can set the button state to selected and then in the next line of code you can call that method (action)

- (IBAction)btn1Touched:(id)sender {
    [_btn2 setSelected:YES];
    [self btn2Touched:self];
}
- (IBAction)btn2Touched:(id)sender {

    NSLog(@"btn Action called");
}
Kasaname
  • 1,491
  • 11
  • 15
0

try like this,'

 -(IBAction)nextbutton:(id)sender{
    UIButton *btn=(UIButton *)[self.view viewWithTag:tagValue];//pass tag value of that particuler button
    btn.selected=YES;
    //do whatevr you want.

    }
0

UIbutton has state called selected and it is managed via property selected

so to make a button to selected state

[buttonInstance setSelected:YES];

To make it unselected use

[buttonInstance setSelected:NO];
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
0

Kindly write a function for that button action like,

-(void)Buttonaction

{

do your action here

  also set your button has selected state.

  buttonname.setselected=yes;

}

call this function while you are tabbing your button.

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
maddysan
  • 258
  • 2
  • 14
0

Do This:

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

    if(sender.isSelected)
      {
        .....
      }
    else
      {
        ...
      }
    [sender setSelected:!sender.isSelected];//This will work as Switch.
}

set Selected(Switch ON) and default(Switch OFF) property (image and title) of UIButton from XIB, OR from Code as you want. With a single button you can do you functionality with this code

Prateek Prem
  • 1,544
  • 11
  • 14