2011-10-17 16 views

risposta

170

Non ho familiarità con una "casella di controllo" in iOS, ma se si utilizza un UISwitch, quindi come visto nell'API di sviluppo, l'attività setOn: animated: dovrebbe fare il trucco.

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

Quindi, per impostare l'interruttore ON nel programma, si usa:

Objective-C

[switchName setOn:YES animated:YES]; 

Swift

switchName.setOn(true, animated: true) 
25

Gli UISwitch hanno una proprietà chiamata "on" che deve essere impostata.

Stai parlando di un'app per iOS o di un sito Web mobile?

+0

ok questo è quello che intendevo. Molte grazie! – Suchi

9

// Usa questo codice ...... // Per risolvere on/off problema di stato interruttore in 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

Io uso anche il setOn:animated: per questo e funziona benissimo. Questo è il codice che uso in viewDidLoad di un'applicazione per alternare uncodice in modo che carichi preset.

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

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

@jamesh Grazie per la straordinaria semplificazione del codice! Molto apprezzato! –

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]; 
    } 
} 
Problemi correlati