如何在Cocoa中动态创建NSCheckbox的标签到Array?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (80)

我为它创建了多个复选框动态和设置标记,现在我想检查它是否已检查,将其标记添加到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];
}

我使用了这段代码,但它将所有内容添加到Array:

-(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");
    }
}

我怎样才能做到这一点?

提问于
用户回答回答于
- (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]];
        }
    }
}

扫码关注云+社区

领取腾讯云代金券

玩转腾讯云 有奖征文活动