我已经完成了以下代码,用于以编程方式创建多个 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 设置为 ON。如何?