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)