-
Notifications
You must be signed in to change notification settings - Fork 1
A 1.2. Creating and Using Switches with UISwitch
rayastar edited this page Feb 7, 2015
·
1 revision
Let’s create a property of type UISwitch and call it mainSwitch:
#import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UISwitch *mainSwitch; @end
Let’s create our switch and place it on our view controller’s view:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mainSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 55, 60)];
[self.view addSubview:self.mainSwitch];
[self.mainSwitch setOn:YES animated:YES];
/* Adjust the off-mode tint color */
self.mainSwitch.tintColor = [UIColor redColor];
/* Adjust the on-mode tint color */
self.mainSwitch.onTintColor = [UIColor brownColor];
/* Also change the knob's tint color */
self.mainSwitch.thumbTintColor = [UIColor greenColor];
if ([self.mainSwitch isOn]) {
NSLog(@"yes");
} else {
NSLog(@"No");
}
[self.mainSwitch addTarget:self action:@selector(switchIsChanged:)
forControlEvents:UIControlEventValueChanged];
}
- (void) switchIsChanged:(UISwitch *)paramSender{
// NSLog(@"Sender is = %@", paramSender);
if ([paramSender isOn]){
NSLog(@"The switch is turned on.");
}else{
NSLog(@"The switch is turned off.");
}