जवाबों:
यदि आप एक यूआईस्विच का उपयोग कर रहे हैं, तो जैसा कि डेवलपर एपीआई में देखा गया है, कार्य setOn: animated:को चाल करना चाहिए।
- (void)setOn:(BOOL)on animated:(BOOL)animated
तो अपने प्रोग्राम में स्विच चालू करने के लिए, आप उपयोग करेंगे:
उद्देश्य सी
[switchName setOn:YES animated:YES];
तीव्र
switchName.setOn(true, animated: true)
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";
}
मैं इसके लिए भी उपयोग करता हूं setOn:animated:और यह ठीक काम करता है। इस कोड को मैं किसी ऐप के में उपयोग है viewDidLoadएक टॉगल करने के लिए UISwitchकोड में इतना है कि यह पूर्व निर्धारित लोड करता है।
// 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;