2011-10-17 4 views

Répondre

170

Je ne suis pas familier avec une 'case à cocher' dans iOS, mais si vous utilisez un UISwitch, alors comme vu dans l'API du développeur, la tâche setOn: animated: devrait faire l'affaire.

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

Donc, pour régler l'interrupteur ON dans votre programme, vous utilisez:

Objective-C

[switchName setOn:YES animated:YES]; 

Swift

switchName.setOn(true, animated: true) 
25

Les UISwitches ont une propriété appelée "on" qui doit être définie.

Parlez-vous d'une application iOS ou d'un site Web mobile?

+0

ok c'est ce que je voulais dire. Merci beaucoup! – Suchi

9

// Utilisation ce code ...... // Pour résoudre marche/arrêt problème d'état dans le commutateur dans iOS

- (IBAction)btnSwitched:(id)sender { 
    UISwitch *switchObject = (UISwitch *)sender; 
    if(switchObject.isOn){ 
     [email protected]"Switch State is Disabled"; 
    }else{ 
     [email protected]"Switch State is Enabled"; 
    }     
2

J'utilise aussi le setOn:animated: pour cela et il fonctionne très bien. C'est le code que j'utilise dans le code viewDidLoad d'une application pour basculer un code UISwitch afin qu'il charge le preset.

// Check the status of the autoPlaySetting 
BOOL autoPlayOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"autoPlay"]; 

[self.autoplaySwitch setOn:autoPlayOn animated:NO]; 
+0

@jamesh Merci pour la simplification impressionnante du code! Très appréciée! –

0

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]; 
    } 
} 
Questions connexes