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

时间:2013-08-08 07:54:11

标签: objective-c macos cocoa

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

我该怎么做?

1 个答案:

答案 0 :(得分:0)

- (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]];
        }
    }
}