Interface file for the view controller - MySwitchViewController.h
1 2 3 4 5 6 7 | #import <UIKit/UIKit.h> @interface MySwitchViewController : UIViewController @property ( nonatomic , strong) UISwitch *mySwitch; @end |
Implementation file for the view controller - MySwitchViewController.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #import "MySwitchViewController.h" @interface MySwitchViewController () @end @implementation MySwitchViewController @synthesize mySwitch; - ( void )viewDidLoad { [ super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //set the view background to white self .view.backgroundColor = [ UIColor whiteColor]; //frame for the switch CGRect myFrame = CGRectMake(10.0f, 10.0f, 250.0f, 25.0f); //create and initialize the slider self .mySwitch = [[ UISwitch alloc] initWithFrame:myFrame]; //set the switch to ON [ self .mySwitch setOn: YES ]; //attach action method to the switch when the value changes [ self .mySwitch addTarget: self action: @selector (switchIsChanged:) forControlEvents: UIControlEventValueChanged ]; [ self .view addSubview: self .mySwitch]; } //check if the switch is currently ON or OFF - ( void ) switchIsChanged:( UISwitch *)paramSender{ if ([paramSender isOn]){ NSLog (@ "The switch is turned on." ); } else { NSLog (@ "The switch is turned off." ); } } - ( void )didReceiveMemoryWarning { [ super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end |
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.