1 //同步属性和成员变量 2 @synthesize mySwitch = _mySwitch; 3 4 - (void)viewDidLoad { 5 [super viewDidLoad]; 6 7 _mySwitch = [[UISwitch alloc]init]; 8 9 //宽高两个属性参数没有用,苹果自己定好了宽高 10 _mySwitch.frame = CGRectMake(100, 100, 40, 40); 11 12 // _mySwitch.on = YES; 13 14 //set方式 15 //[_mySwitch setOn:YES]; 16 17 [_mySwitch setOn:YES animated:YES]; 18 19 //设置开启状态时风格 20 //[_mySwitch setOnTintColor:[UIColor blueColor]]; 21 22 //圆钮 23 [_mySwitch setThumbTintColor:[UIColor redColor]]; 24 //整体 25 [_mySwitch setTintColor:[UIColor blackColor]]; 26 27 [self.view addSubview:_mySwitch]; 28 29 30 [_mySwitch addTarget:self action:@selector(swchanged:) forControlEvents:UIControlEventValueChanged]; 31 32 33 } 34 35 -(void) swchanged:(UISwitch*) sw 36 { 37 if(sw.on) 38 { 39 NSLog(@"Sw open"); 40 } 41 else 42 { 43 NSLog(@"Sw closed"); 44 } 45 46 47 }
1 @interface ViewController : UIViewController 2 { 3 //定义一个开关控件 4 UISwitch* _mySwitch ; 5 } 6 7 @property (retain,nonatomic) UISwitch* mySwitch;
时间: 2024-11-08 21:25:56