用处:开关空间,用于处理两种状态
-(void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(20,20,0,0);
self.switch = [[UISwitch alloc]initWithFrame:frame];
//设置初始状态
[self.switch setOn:YES];
//底色颜色
self.switch.tintColor = [UIColor redColor];
//开启底色颜色
self.switch.ontintColor = [UIColor brownColor];
//滑块颜色
self.switch.thumbtintColor = [UIColor greenColor];
//添加值改变事件方法
[self.switch addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
[self.view addSubView:self.switch];
}
-(IBAction) change:(id)sender
{
UISwitch* switch = (UISwitch*)sender;
if(switch.on)
{NSLog(@"set on");}
else
{NSLog(@"set off");}
}
原文:http://www.cnblogs.com/AngryCooder/p/3894774.html