代表开关按钮 可配置选项很少,只适用于处理布尔值 可通过监控该空间的ValueChanged事件来检测开关按钮的状态切换,也可通过属性on或实例方法isOn来获取当前值
UISwitch继承了UIControl基类
state-Off On
UISwitch* modleSwitch=[[UISwitch alloc]initWithFrame:CGRectMake(50, 370, 50, 30)];
[modleSwitch addTarget:self action:@selector(changeBackground:) forControlEvents:UIControlEventValueChanged];
[modleSwitch setOn:YES];//设置开关的初始状态
[self.view addSubview:modleSwitch];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)changeBackground:(id)sender{
NSLog(@"312");
if ([sender isOn]==YES) {
self.view.backgroundColor=[UIColor whiteColor];
}else{
self.view.backgroundColor=[UIColor purpleColor];
}
}
网友评论