Réponses:
Si vous utilisez un UISwitch, alors comme on le voit dans l'API développeur, la tâche setOn: animated:devrait faire l'affaire.
- (void)setOn:(BOOL)on animated:(BOOL)animated
Donc, pour activer l'interrupteur dans votre programme, vous utiliseriez:
Objectif c
[switchName setOn:YES animated:YES];
Rapide
switchName.setOn(true, animated: true)
Utilisez ce code pour résoudre le problème d'état d'activation / désactivation dans le commutateur sous 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";
}
J'utilise également le setOn:animated:pour cela et cela fonctionne très bien. C'est le code que j'utilise dans une application viewDidLoadpour basculer UISwitchdans un code afin qu'il charge le préréglage.
// Check the status of the autoPlaySetting
BOOL autoPlayOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"autoPlay"];
[self.autoplaySwitch setOn:autoPlayOn animated:NO];
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];
}
}
UISwitch *switchObject = (UISwitch *)sender;