Respostas:
Se você estiver usando um UISwitch, conforme visto na API do desenvolvedor, a tarefa setOn: animated:deve funcionar.
- (void)setOn:(BOOL)on animated:(BOOL)animated
Portanto, para definir a chave ON em seu programa, você usaria:
Objective-C
[switchName setOn:YES animated:YES];
Rápido
switchName.setOn(true, animated: true)
Use este código para resolver o problema de estado ligado / desligado na chave no 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";
}
Eu também uso o setOn:animated:para isso e funciona bem. Este é o código que uso em um aplicativo viewDidLoadpara alternar entre um UISwitchcódigo e carregá-lo.
// 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;