How to Use and listen to uiswitch controls for iOS Learning

Source: Internet
Author: User

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.
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.