The first way to create a uiswitch component is to create it dynamically in your code.
1, open Xcode, new project switch, select Single View application.
2, open the viewcontroller.m file in the Viewdidload method to add code:
Copy Code code as follows:
(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 no additional setup after loading the view, typically from a nib.
}
[Switchbutton addtarget:selfaction: @selector (switchaction:) forcontrolevents:uicontroleventvaluechanged];
Switchaction in Selector in code: we need to implement it ourselves, which is the event received when we press.
Remember to add Switchbutton to the current view and call [Self.viewaddSubview:switchButton];
3, monitor Uiswitch Press the event
The implementation code is as follows:
Copy Code code 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 what I do by dragging the control method onto the label on the interface to facilitate the display effect
Run, Effect:
Using Uiswitch by drag-and-drop method
1. Drag a Uiswitch control to the Xib file.
2, press Alt+command + Return key to open Assistant editor mode, select the Uiswitch control, press and hold, and drag to ViewController.h
3. Select action Mode
4. m file to implement Switchaction. Just the dynamic creation of the time also used this method name, you can first comment out just.
Copy Code code as follows:
(ibaction) Switchaction: (ID) Sender {
Uiswitch *switchbutton = (uiswitch*) sender;
BOOL Isbuttonon = [Switchbutton ISON];
if (Isbuttonon) {
Showswitchvalue.text = @ "yes";
}else {
Showswitchvalue.text = @ "No";
}
}
Three, custom Uiswitch
1. Use category extension Uiswitch.
As follows:
Here is the UISwitch.h file:
Copy Code code as follows:
#import
@interface Uiswitch (tagged)
+ (Uiswitch *) Switchwithlefttext: (NSString *) Tag1 andright: (NSString *) Tag2;
@property (nonatomic, readonly) Uilabel *label1;
@property (nonatomic, readonly) Uilabel *label2;
@end
UISWITCH.M file:
Copy Code code as follows:
#import "Uiswitch-extended.h"
#define TAG_OFFSET 900
@implementation Uiswitch (tagged)
-(void) Spelunkandtag: (UIView *) Aview withcount: (int *) count
{
For (UIView *subview in [Aview subviews])
{
if ([Subview Iskindofclass:[uilabel class]])
{
*count + 1;
[Subview Settag: (Tag_offset + *count)];
}
Else
[Self Spelunkandtag:subview withcount:count];
}
}
-(Uilabel *) Label1
{
Return (Uilabel *) [self viewwithtag:tag_offset + 1];
}
-(Uilabel *) Label2
{
Return (Uilabel *) [self Viewwithtag:tag_offset + 2];
}
+ (Uiswitch *) Switchwithlefttext: (NSString *) Tag1 andright: (NSString *) Tag2
{
Uiswitch *switchview = [[Uiswitch alloc] Initwithframe:cgrectzero];
int labelcount = 0;
[SwitchView Spelunkandtag:switchview withcount:&labelcount];
if (Labelcount = 2)
{
[Switchview.label1 SETTEXT:TAG1];
[Switchview.label2 Settext:tag2];
}
return [SwitchView autorelease];
}
@end
2. There is another way, this method is relatively simple, but more difficult to understand, I do not understand.
Copy Code code as follows:
Uiswitch *isfooorbar=[[uiswitch alloc] init];
((Uilabel) [[[[Isfooorbar Subviews] lastobject] subviews] objectatindex:2] subviews]objectatindex:0]). Text = @ "Foo";
((Uilabel *) [[[[Isfooorbar Subviews] lastobject] subviews] objectatindex:2] subviews]objectatindex:1]). Text = @ "Bar" ;*
Four, some common methods
Get switch status
Copy Code code as follows:
BOOL setting = Switchview.ison;
NSLog (@ "%d", setting);
Set switch status no off state, yes open state
Copy Code code as follows:
[SwitchView seton:setting Animated:yes];
Set the switch for the opening
Copy Code code as follows:
Switchview.ontintcolor = [Uicolor Orangecolor];
Set the color of a button
Copy Code code as follows:
Switchview.thumbtintcolor = [Uicolor Redcolor];
Color of the border of a switch control
Copy Code code as follows:
Switchview.tintcolor = [Uicolor Purplecolor];
Add Trigger Event
Copy Code code as follows:
[SwitchView addtarget:self Action: @selector (switchaction:) forcontrolevents:uicontroleventvaluechanged];