Uso de los interruptores de IOS

Los interruptores se utilizan para alternar entre los estados de encendido y apagado.

Propiedades importantes

  • onImage
  • offImage
  • on

Métodos importantes

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

Agregar el método personalizado addSwitch y el interruptor

-(IBAction)switched:(id)sender{
    NSLog(@"Switch current state %@", mySwitch.on ? @"On" : @"Off");
}
-(void)addSwitch{
    mySwitch = [[UISwitch alloc] init];
    [self.view addSubview:mySwitch];
    mySwitch.center = CGPointMake(150, 200);
    [mySwitch addTarget:self action:@selector(switched:)
    forControlEvents:UIControlEventValueChanged];    
}

En ViewController.m, modifique viewDidLoad como se muestra a continuación.

(void)viewDidLoad
{
   [super viewDidLoad];
   [self addSwitch];
}

Salida

Ahora, al ejecutar la aplicación, veremos la siguiente salida:

switchOutput1

Al deslizar el interruptor hacia la derecha, la salida es la siguiente:

switchOutput2

Otras extensiones