IOS Uiswitch Controls

Source: Internet
Author: User

A brief introduction to Uiswitch

The role of Uiswitch is to provide the user with a switch, which is very common in the Setup interface of the system, and the controls are simple.


two uiswitch creation
Create Uiswitch *switch1 = [[Uiswitch alloc]init];        Cgsize viewsize = Self.view.bounds.size;switch1.frame = CGRectMake (viewsize.height*0.2, 150, 0, 0); Initializes the switch control using the initWithFrame method. CGRect rect = CGRectMake (viewsize.height*0.2, 250, 0, 0); Uiswitch *SWITCH2 = [[Uiswitch alloc]initwithframe:rect];


Three sets the selected state
@property (Nonatomic,getter=ison) BOOL on;

The on property is used to control the state of the switch, and if set to Yes means open, if no it is closed and can be judged by the ISON side.

1 Setting the switching state//1.1 setOn method [Switch1 Seton:yes]; 1.2 seton:animated: Method. The animated parameter is a Boolean type, and if the value is YES, the animation is displayed when the switch changes state [SWITCH2 seton:yes ANIMATED:YES]//2 judgment Status if ([Switch1 isOn]) {NSLog (@ "The switch i s on. "); else {NSLog (@ "The switch is off.");



Quad Add monitor

If you want to be notified when the switch control is turned on or off, you can use the Uiswitch addTarget:action:forControlEvents: Method plus the switch target.

1. Add listener [Switch1 addtarget:self action: @selector (switchischanged:) forcontrolevents:uicontroleventvaluechanged];//2. The method that executes after an event occurs/** * Switchischanged method for listening for Uiswitch control's value change * * @param swith swith control */-(void) switchischanged: (Uiswitch *) SWI th{if ([Swith isOn]) {NSLog (@ "The switch is on."); else {NSLog (@ "The switch is off.");}}


Five Test code5.1 Code
#import   "ViewController.h" @interface  ViewController  () @end @implementation viewcontroller-   (void) viewdidload {    [super viewdidload];    //1. Create     UISwitch *switch1 = [[UISwitch alloc]init];     cgsize viewsize = self.view.bounds.size;    switch1.frame =  cgrectmake (viewsize.height*0.2, 150, 0, 0);             //1.1 use the  initWithFrame  method to initialize the switch control.         cgrect rect = cgrectmake (viewSize.height*0.2,  250, 0, 0);     uiswitch *switch2 = [[uiswitch alloc] initwithframe:rect];                 //2  Set the default check     //@property (nonatomic,gEtter=ison)  BOOL on;    [switch1 setOn:YES];         //2.1 seton:animated: Method. The animated  parameter is a Boolean type and will show   animate if the value is  YES  switch state is changed     [switch2 seton:yes  animated:yes];            //3. Determine if   is selected    if  ([Switch1 ison]) {        nslog (@ "the  switch is on. ");     } else {        nslog (@ "The  Switch is off. ");     }            // 4 If you want to be notified when the switch control is turned on or off, you must use the  UISwitch  addTarget:action:forControlEvents: Method plus the  target of the switch in your class. The following code:     [switch1 addtarget:self action: @selector (switchischanged:)   Forcontrolevents:uicontroleventvaluechanged];        //Add to Uiview    [self.view addsubview: switch1];    [self.view addsubview:switch2];    }/** *   switchIsChanged  method for monitoring the value change of the Uiswitch control  * *   @param  swith swith  control  */-(void) switchischanged: (uiswitch *) swith{    if  ([Swith isOn]) {         nslog (@ "The switch is on.");     } else {        nslog (@ "The  Switch is off. ");     }    }-  (void) didreceivememorywarning {     [super didreceivememorywarning];    // dispose of any  resources that can be recreated.} @end


5.2 Execution Results
2015-04-07 00:00:59.435 2uiswitch[1220:29996] The switch is on.2015-04-07 00:01:06.134 2uiswitch[1220:29996] The switch i S off.2015-04-07 00:01:08.424 2uiswitch[1220:29996] The switch is on.2015-04-07 00:11:57.685 2uiswitch[1220:29996] the SW Itch is off.2015-04-07 00:12:03.681 2uiswitch[1220:29996] The switch is on.2015-04-07 00:12:04.219 2uiswitch[1220:29996] The switch is off.2015-04-07 00:12:04.965 2uiswitch[1220:29996], the switch is on.

IOS Uiswitch Controls

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.