Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

iOS Switches (Switches)

Use of IOS Switches

A switch is used to toggle between on and off states.

Important Properties

Important Methods



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

Add the custom method addSwitch and the switch



-(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];    

}

Modify viewDidLoad in ViewController.m as follows



(void)viewDidLoad

{

   [super viewDidLoad];

   [self addSwitch];

}

Output

Now when we run the application we will see the following output

switchOutput1

The output of sliding the switch to the right is as follows

switchOutput2