12

I have a settings view in my app which has a couple of buttons (actually UISwitches). If the "off" setting on one of the switches is selected, I'd like to hide the second switch immediately. Can this be done?

Jack BeNimble
  • 35,733
  • 41
  • 130
  • 213

4 Answers4

32
IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;

write the above 2 lines in your .h file and set the outlets with XIB.

Now create a method called hideButton

-(IBAction)hideButton
{
       btn1.hidden = YES;
}

in XIB attach this method with btn2. So now when you click on btn2 it will hide btn1.

Michael Ho Chum
  • 939
  • 8
  • 17
Gabriel
  • 3,039
  • 6
  • 34
  • 44
4

Connect the two switches as outlets. lets say switch1 & switch2.

Connect this function to the valueChanged action.

- (IBAction)mySwitch1:(id)sender { 
    [switch2 setHidden:!(switch1.isOn)];
}

Now when switch1 is not on then switch2 will be hidden.

NJones
  • 27,139
  • 8
  • 70
  • 88
3

Add a target to the first switch which on value change calls the instance of the second switch and hides it.

Add the target:

    [switch1 addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventValueChanged];

Calls this method:

- (void) switchToggled:(UISwitch*)switch {
 if ([switch isOn]) switch2.hidden = YES;
 else switch2.hidden = NO;
}

NJones if statement is more effective though.

Wolfert
  • 974
  • 6
  • 12
0

Swift 4


Within your function do the following:

btn1.isHidden = true
thalacker
  • 2,389
  • 3
  • 23
  • 44