The switch is used to switch between the on and off states.
Important attribute
OnImage
OffImage
On
9.24.1. An important method ¶
- (void)setOn:(BOOL)on animated:(BOOL)animated
9.24.2. Add custom methods addSwitch and switches ¶
-(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];
}
9.24.3. Modify the 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
The output of the right slide switch is as follows