本文介绍了如何将可动态创建的NSCheckbox的标签添加到Cocoa中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created Multiple checkbox dynamic and set tag for it, Now I want to check if it checked, add its tag to NSArray. This is my code to create mutiple 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];
}

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

I used this code but it add all to 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]];
        }
    }
}

这篇关于如何将可动态创建的NSCheckbox的标签添加到Cocoa中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 01:25