2011-10-17 10 views

Respuesta

170

No estoy familiarizado con una 'casilla de verificación' en iOS, pero si está usando un UISwitch, entonces como se ve en la API de desarrollador, la tarea setOn: animated: debería ser la solución.

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

Así que para establecer el interruptor de encendido en su programa, se debería utilizar:

Objective-C

[switchName setOn:YES animated:YES]; 

Swift

switchName.setOn(true, animated: true) 
25

Los UISwitches tienen una propiedad llamada "on" que se debe establecer.

¿Estás hablando de una aplicación iOS o un sitio web móvil?

+0

ok, eso es lo que quise decir. ¡Muchas gracias! – Suchi

9

// Uso este código ...... // Para resolver el problema de estado/apagado en el interruptor en 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

También utilizo el setOn:animated: para este y funciona bien. Este es el código que uso en viewDidLoad de una aplicación para alternar un UISwitch en el código para que cargue el preajuste.

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

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

@jamesh ¡Gracias por la increíble simplificación de código! ¡Muy apreciado! –

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]; 
    } 
} 
Cuestiones relacionadas