Learn IOS switch button Uiswitch control _ios

Source: Internet
Author: User
Tags uikit

Switch button Uiswitch
Inside the ViewController.h.

#import <UIKit/UIKit.h>

@interface viewcontroller:uiviewcontroller{

  //define a switch control
  /function can be changed in state
  //On, off: both states can be switched//
  all controls in the Uikit frame library are already UI started
  //Apple's official controls are defined in the Uikit frame library
  uiswitch * _myswitch;

}

@property (retain,nonatomic) uiswitch * myswitch;


@end

Inside the VIEWCONTROLLER.M.

#import "ViewController.h" @interface Viewcontroller () @end @implementation Viewcontroller @synthesize MYSWITCH=_MYSW

Itch;
  -(void) viewdidload {[Super viewdidload];

  Do no additional setup after loading the view, typically from a nib.

  Creates a switch object//inherits from the UIView _myswitch = [[Uiswitch alloc]init]; Apple's official control position setting//Position X,y value can be changed (100,100)//width and height value cannot be changed (80,40) written and useless, will not play a role.
  The default.

  _myswitch.frame=cgrectmake (100, 200, 180, 40);

  Switch State settings Property//yes: Open State//no: Off state _myswitch.on=yes;

  You can also use the Set function//[_myswitch Seton:yes];

  Set the switch state//p1: State settings//P2: Whether the animation effect is open//[_myswitch seton:yes animated:yes];

  [Self.view Addsubview:_myswitch];

  Set the style color of the open state [_myswitch Setontintcolor:[uicolor Orangecolor]];

  Set switch round button style color [_myswitch setthumbtintcolor:[uicolor Bluecolor]];

  Set the overall style color, the white of the button is the background color of the entire parent layout [_myswitch Settintcolor:[uicolor Greencolor]]; To add an event function to a switch control//P1: function Implements Object//P2: Function object//p3: Event type when event response uicontroleventvaluechanged when state changes when a function is triggered [_myswitchAddtarget:self Action: @selector (Swchange:) forcontrolevents:uicontroleventvaluechanged];
  }//Parameter incoming switch object itself-(void) Swchange: (uiswitch*) sw{if (sw.on==yes) {NSLog (@ "switch is turned on");
  }else{NSLog (@ "switch is off");
  }-(void) didreceivememorywarning {[Super didreceivememorywarning];
Dispose of any of the can is recreated.
 } @end

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.