Switch button Uiswitch
Inside the ViewController.h.
#import <UIKit/UIKit.h>
@interface viewcontroller:uiviewcontroller{
//define a switch control
/function can be changed in state
//On, off: both states can be switched//
all controls in the Uikit frame library are already UI started
//Apple's official controls are defined in the Uikit frame library
uiswitch * _myswitch;
}
@property (retain,nonatomic) uiswitch * myswitch;
@end
Inside the VIEWCONTROLLER.M.
#import "ViewController.h" @interface Viewcontroller () @end @implementation Viewcontroller @synthesize MYSWITCH=_MYSW
Itch;
-(void) viewdidload {[Super viewdidload];
Do no additional setup after loading the view, typically from a nib.
Creates a switch object//inherits from the UIView _myswitch = [[Uiswitch alloc]init]; Apple's official control position setting//Position X,y value can be changed (100,100)//width and height value cannot be changed (80,40) written and useless, will not play a role.
The default.
_myswitch.frame=cgrectmake (100, 200, 180, 40);
Switch State settings Property//yes: Open State//no: Off state _myswitch.on=yes;
You can also use the Set function//[_myswitch Seton:yes];
Set the switch state//p1: State settings//P2: Whether the animation effect is open//[_myswitch seton:yes animated:yes];
[Self.view Addsubview:_myswitch];
Set the style color of the open state [_myswitch Setontintcolor:[uicolor Orangecolor]];
Set switch round button style color [_myswitch setthumbtintcolor:[uicolor Bluecolor]];
Set the overall style color, the white of the button is the background color of the entire parent layout [_myswitch Settintcolor:[uicolor Greencolor]]; To add an event function to a switch control//P1: function Implements Object//P2: Function object//p3: Event type when event response uicontroleventvaluechanged when state changes when a function is triggered [_myswitchAddtarget:self Action: @selector (Swchange:) forcontrolevents:uicontroleventvaluechanged];
}//Parameter incoming switch object itself-(void) Swchange: (uiswitch*) sw{if (sw.on==yes) {NSLog (@ "switch is turned on");
}else{NSLog (@ "switch is off");
}-(void) didreceivememorywarning {[Super didreceivememorywarning];
Dispose of any of the can is recreated.
} @end
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.