UISwitch Control for IOS development (2)

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.