0

我创建了多个动态复选框并为其设置标签,现在我想检查它是否选中,将其标签添加到 NSArray。这是我创建多个 NSCheckbox 的代码:

for(int i = 1; i <= number; i++)
{
    DeleteCheckbox = [[NSButton alloc] initWithFrame:NSMakeRect (20,textfield_Y,50,25)];
    [DeleteCheckbox setButtonType:NSSwitchButton];
    [DeleteCheckbox setBezelStyle:0];
    [DeleteCheckbox setTitle:@""];
    [DeleteCheckbox setTag:200+i];
    [DeleteCheckbox setState:NSOffState];
    [DeleteCheckbox setAction:@selector(checkState:)];
    [guiView addSubview:DeleteCheckbox];
    [DeleteCheckbox release];
}

我使用了这段代码,但它全部添加到数组中:

-(IBAction)checkState:(NSButton *)sender
{
    if ([sender state] == NSOnState) {
        NSLog(@"selected");
        for (int i = 1; i <= number; i ++) {
            [deleteSelectedArray addObject: [[guiView viewWithTag:200+i] intValue]];

        }
        NSLog(@"deleteSelectedArray : %@",deleteSelectedArray);

    }
    else {
        NSLog(@"not selected");
    }
}

我怎样才能做到这一点?

4

1 回答 1

1
- (IBAction) checkState: (NSButton*) sender {
    for (int i = 1; i <= number; i ++) {
        if ([[guiView viewWithTag: 200 + i] state] == NSOnState) {
            [deleteSelectedArray addObject: [[guiView viewWithTag: 200 + i] intValue]];
        }
    }
}
于 2013-09-11T19:52:09.190 回答