我创建了多个动态复选框,现在我想检查它们的状态,但我不知道该怎么做。这是我创建多个 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:(id)sender
{
if ([DeleteCheckbox state] == NSOnState) {
NSLog(@"selected");
}
else {
NSLog(@"not selected");
}
}
但是在运行时,它总是 printf:"not selected"。