1. Preface
The switch space is also very common in IOS. Let's introduce its simple usage today.
2 UISwitch Introduction
The switch space is similar to the HTML single-choice button. There are only two States: ON/OFF. the following key code is as follows:
. H file:
[Plain]
@ Property (nonatomic, strong) UISwitch * mySwitch;
@ Property (nonatomic, strong) UISwitch * mySwitch;. m file:
[Plain] view plaincopyprint? @ Synthesize mySwitch;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
// Initialize the switch control, CGRectMake (x, y, width, and height)
Self. mySwitch = [[UISwitch alloc] initWithFrame: CGRectMake (100,100, 0, 0)];
// Set the switch to YES
// [Self. mySwitch setOn: YES];
// Add events for controls
[Self. mySwitch addTarget: self action: @ selector (switchIsChanged :) forControlEvents: UIControlEventValueChanged];
// Add the control to the view
[Self. view addSubview: self. mySwitch];
}
-(Void) switchIsChanged :( UISwitch *) paramSender {
NSLog (@ "Sender is =%@", paramSender );
If ([paramSender isOn]) {// if the switch status is ON
NSLog (@ "The switch is turned on .");
} Else {
NSLog (@ "The switch is turned off .");
}
}
@ Synthesize mySwitch;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
// Initialize the switch control, CGRectMake (x, y, width, and height)
Self. mySwitch = [[UISwitch alloc] initWithFrame: CGRectMake (100,100, 0, 0)];
// Set the switch to YES
// [Self. mySwitch setOn: YES];
// Add events for controls
[Self. mySwitch addTarget: self action: @ selector (switchIsChanged :) forControlEvents: UIControlEventValueChanged];
// Add the control to the view
[Self. view addSubview: self. mySwitch];
}
-(Void) switchIsChanged :( UISwitch *) paramSender {
NSLog (@ "Sender is =%@", paramSender );
If ([paramSender isOn]) {// if the switch status is ON
NSLog (@ "The switch is turned on .");
} Else {
NSLog (@ "The switch is turned off .");
}
}
The running result is as follows:
If ON/OFF is selected, the console displays:
13:46:51. 430 UISwitchViewControllerTest [540: c07] Sender is = <UISwitch: 0x752d530; frame = (100 100; 79 27); layer = <CALayer: 0x752e430>
13:46:51. 431 UISwitchViewControllerTest [540: c07] The switch is turned on.
13:46:58. 877 UISwitchViewControllerTest [540: c07] Sender is = <UISwitch: 0x752d530; frame = (100 100; 79 27); layer = <CALayer: 0x752e430>
13:46:58. 878 UISwitchViewControllerTest [540: c07] The switch is turned off.