1. The first method to create the uiswitch control is as follows:Code.
1. Open xcode 4.3.2, create a project switch, and select single view application.
2. Open the viewcontroller. M file and add the code in the viewdidload method:
-(Void) viewdidload {[Super viewdidload]; uiswitch * switchbutton = [[uiswitch alloc] initwithframe: cgrectmake (50,100, 20, 10)]; [switchbutton Seton: Yes]; [switchbutton addtarget: Self action: @ selector (switchaction :) forcontrolevents: uicontroleventvaluechanged]; [self. view addsubview: switchbutton]; // do any additional setup after loading the view, typically from a nib .}
[SwitchbuttonAddtarget:SelfAction:@ Selector(Switchaction :)Forcontrolevents:Uicontroleventvaluechanged];
Switchaction in selector in the Code: we need to implement it ourselves, that is, the event received when we press it.
Remember to add the switchbutton to the current view and call [Self.ViewAddsubview: Switchbutton];
3. Listening for uiswitch push events
The implementation code is as follows:
-(Void) switchaction :( ID) sender {uiswitch * switchbutton = (uiswitch *) sender; bool isbuttonon = [switchbutton ISON]; If (isbuttonon) {showswitchvalue. TEXT = @ "yes";} else {showswitchvalue. TEXT = @ "no ";}}
Showswitchvalue is the label that I put on the interface by dragging the control method, to facilitate the Display Effect
Running, effect:
2. Use uiswitch by dragging
1. Drag a uiswitch control to the XIB file.
2. press Alt + command + return to enable the assistant editor mode, select the uiswitch control, hold down the control key, and drag it to viewcontroller. h.
3. Select action
4. Implement switchaction in the. M file. This method name is also used during the dynamic creation. you can comment out the method just now.
-(Ibaction) switchaction :( ID) sender {uiswitch * switchbutton = (uiswitch *) sender; bool isbuttonon = [switchbutton ISON]; If (isbuttonon) {showswitchvalue. TEXT = @ "yes";} else {showswitchvalue. TEXT = @ "no ";}}
Run it.
Example code: https://github.com/schelling/YcDemo
Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you.