IOS - How to Install UISwitch Programmatically

I want my UISwitch to turn on or off programmatically. How should I do it? I am new to iOS.

+83
ios objective-c
Oct. 17 '11 at 21:25
source share
5 answers

I am not familiar with the 'checkbox' in iOS, but if you use UISwitch, then as you can see from the development API, the setOn: animated: task should do the trick.

 - (void)setOn:(BOOL)on animated:(BOOL)animated 

So, to set the switch to your program, you should use:

Objective-c

 [switchName setOn:YES animated:YES]; 

Swift

 switchName.setOn(true, animated: true) 
+166
Oct 17 2018-11-21T00:
source share

UISwitches have a property called "on" that must be set.

Are you talking about an iOS application or a mobile website?

+24
Oct 17 '11 at 21:28
source share

// Use this code ...... // To solve the on / off problem when switching to iOS

 - (IBAction)btnSwitched:(id)sender { UISwitch *switchObject = (UISwitch *)sender; if(switchObject.isOn){ self.lblShow.text=@"Switch State is Disabled"; }else{ self.lblShow.text=@"Switch State is Enabled"; } 
+9
Apr 25 '14 at 6:56
source share

I also use setOn:animated: for this, and it works fine. This is the code that I use in the viewDidLoad application to switch a UISwitch into code so that it loads the preset.

 // Check the status of the autoPlaySetting BOOL autoPlayOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"autoPlay"]; [self.autoplaySwitch setOn:autoPlayOn animated:NO]; 
+2
Sep 08 '14 at 18:23
source share

ViewController.h

 - (IBAction)switchAction:(id)sender; @property (strong, nonatomic) IBOutlet UILabel *lbl; 

ViewController.m

 - (IBAction)switchAction:(id)sender { UISwitch *mySwitch = (UISwitch *)sender; if ([mySwitch isOn]) { self.lbl.backgroundColor = [UIColor redColor]; } else { self.lbl.backgroundColor = [UIColor blueColor]; } } 
0
Jun 20 '16 at 4:53 on
source share



All Articles