下面演示如何创建开关,以及监听它值的改变,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class ViewController : UIViewController { var uiswitch: UISwitch !; override func viewDidLoad() { super .viewDidLoad() uiswitch = UISwitch () //设置位置(开关大小无法设置) uiswitch.center= CGPointMake (100,50); //设置默认值 uiswitch.on= true ; uiswitch.addTarget( self , action: Selector ( "switchDidChange" ), forControlEvents: UIControlEvents . ValueChanged ) self .view.addSubview(uiswitch); } func switchDidChange(){ //打印当前值 println (uiswitch.on) } } |