我已经完成了以下代码,用于以编程方式创建多个UISwitches并处理特定的开关。
for (int i =0; i < 3; i++) {
CGRect frame = CGRectMake(x, y, height, width);
UISwitch *switchControl = [[UISwitch alloc] initWithFrame:frame];
//add tag as index
switchControl.tag = i;
[switchControl addTarget:self action:@selector(flip:) forControlEvents: UIControlEventValueChanged];
[switchControl setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:switchControl];
y= y+50.0;
}
- (IBAction) flip: (id) sender {
UISwitch *onoff = (UISwitch *) sender;
NSLog(@"no.%d %@",onoff.tag, onoff.on ? @"On" : @"Off");
//use onoff.tag , you know which switch you got
}完成这段代码后,我希望当UIButton选择全部单击时,将所有UISwitches设置为打开。多么?
发布于 2012-05-21 19:25:14
要将它们全部设置为ON,我会将它们保存在一个数组中,以便于访问。然后你可以这样做:
for (int i = 0; i < [switchArray count]; i++) {
UISwitch *sw = (UISwitch *)[switchArray objectAtIndex:i];
[sw setOn:YES];
}你也可以这样做:
for (int i = 0; i < 3; i++) {
UISwitch *sw = (UISwitch *)[self.view viewWithTag:i];
[sw setOn:YES];
}只要确保标签是唯一的即可。
希望能有所帮助。
发布于 2012-05-21 19:29:49
使用这个东西,请以100或1000开头的标签号
-(void) selectAll {
for(int i=100;i<(100+3);i++){
UISwitch *refSwitch=[self.view viewWithTag:i];
refSwitch.on=YES;
}
}这对你来说很有用
https://stackoverflow.com/questions/10684200
复制相似问题