Yanıtlar:
Bir UISwitch kullanıyorsanız, geliştirici API'sinde görüldüğü gibi, görev setOn: animated:hile yapmalıdır.
- (void)setOn:(BOOL)on animated:(BOOL)animated
Bu nedenle, programınızda anahtarı AÇIK konuma getirmek için şunları kullanırsınız:
Amaç-C
[switchName setOn:YES animated:YES];
Swift
switchName.setOn(true, animated: true)
İOS'taki anahtardaki açma / kapama durumu sorununu çözmek için bu kodu kullanın
- (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";
}
Bunun için de kullanıyorum setOn:animated:ve iyi çalışıyor. Bu, bir uygulamanın ön ayarı yükleyecek şekilde kodda viewDidLoadgeçiş yapmak için kullandığım UISwitchkoddur.
// 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;