gpt4 book ai didi

objective-c - 如何将动态创建的 NSCheckbox 标签添加到 Cocoa 中的数组中

转载 作者:行者123 更新时间:2023-12-03 17:53:28 24 4
gpt4 key购买 nike

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

我怎样才能做到这一点?

最佳答案

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

关于objective-c - 如何将动态创建的 NSCheckbox 标签添加到 Cocoa 中的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18120748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com