iOS development tips-using and customizing switch controls (Uiswitch)

Source: Internet
Author: User
Tags event listener

1. Initialize the load to the view interface

(Swift)

Import UIKitclass Viewcontroller:uiviewcontroller {    // 1. Create a property of type Uiswitch    var mainswitch:uiswitch!        override//0)) View.addsubview (Mainswitch)}}       

(OBJECTIVE-C)

#import"ViewController.h"@interface Viewcontroller () // 1. Create a property of type Uiswitch@property (Nonatomic, strong) Uiswitch *mainswitch; @end  @implementation  viewcontroller-(void// 2. Create switch Self.mainswitch = [[Uiswitch alloc] Initwithframe:cgrectmake (100, 100, 0, 0)]; [Self.view AddSubview:self.mainSwitch];}  @end             

2. Setting the switch status

(Swift)

Mainswitch.seton (true)

(OBJECTIVE-C)

[Self.mainswitch Seton:yes];

3. Determine the switching status

(Swift)

If mainswitch.on{/* * */else/*/    }      

(OBJECTIVE-C)

If ([Self.mainswitch isOn]) {    NSLog (@ "The switch is on.") else {NSLog (@ "Theswitch is off.") );}

4. Add Event Listener

(Swift)

Mainswitch.addtarget (Self, Action:"switchischanged:", forControlEvents:. valuechanged) func switchischanged (sender:uiswitch) {println ("sender is = \ (sender)") if s ender.on{println ("Theswitch is turned on")} else {println ("Theswitch is Turne D off")}}                

(OBJECTIVE-C)

[Self.mainswitch addtarget:self Action: @selector (switchischanged:) forcontrolevents:uicontroleventvaluechanged]; - (void) Switchischanged: (Uiswitch *) Paramsender {NSLog (@ "Sender is =%@", Paramsender); if ([Paramsender isOn]) {NSLog (@ "Theswitch is turned on." ); } Else {NSLog (@ "Theswitch is turned off.") ); }}

5. Customizing the Switch UI

/* */mainswitch.tintcolor = uicolor.redcolor ()/**/Mainswitch.ontintcolor =  Uicolor.browncolor ()/**/Mainswitch.thumbtintcolor = Uicolor.greencolor ()    

iOS development tips-using and customizing switch controls (Uiswitch)

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.