1

プログラムで複数の 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 をオンに設定します。どのように?

4

2 に答える 2

2

それらをすべて設定する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-21T11:25:14.167 に答える
0

タグ番号は 100 か 1000 で始めてください

-(void) selectAll {
for(int i=100;i<(100+3);i++){
UISwitch *refSwitch=[self.view viewWithTag:i];
refSwitch.on=YES;
}
}

これはあなたのために働くでしょう

于 2012-05-21T11:29:49.120 に答える