Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

objective c - How to Add tag of NSCheckbox created dynamically to Array in Cocoa

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

How can I do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
- (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]];
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...