1

我的 NSMatrix 中有两个单选按钮,模式为 NSRadioModeMatrix。默认情况下,单击第一个单选按钮。我的问题是当我点击我的第二​​个单选按钮“让我选择”并点击取消时,两个单选按钮似乎都被选中了。当单击“选择文件夹”对话框中的“取消”时,我试图取消选择我的第二个单选按钮。选择路径并选择打开时,它可以正常工作。使用 NSRadioModeMatrix,它必须一次选择一个单选按钮。但是为什么一次选择两个按钮。我在这里做错了什么

 NSButtonCell *prototype = [[NSButtonCell alloc] init];
[prototype setTitle:@"Choose home Folder"];
[prototype setButtonType:NSRadioButton];

NSRect matrixRect  = NSMakeRect(15,150,450,125);

myMatrix = [[NSMatrix alloc] initWithFrame:matrixRect mode:NSRadioModeMatrix
                                           prototype:(NSCell *)prototype
                                        numberOfRows:2
                                     numberOfColumns:1];
NSSize cellSize;
cellSize.height =40;
cellSize.width=400;

[myMatrix setCellSize:cellSize];
[myMatrix setTarget:self];
[myMatrix setAction:@selector(HandleRadioClick)];

NSArray *cellArray = [myMatrix cells];
[[cellArray objectAtIndex:0] setTitle:@"Leave it as Default"];
[[cellArray objectAtIndex:0] setTag:0];
[[cellArray objectAtIndex:1] setTitle:@"Let me Choose"];
[[cellArray objectAtIndex:1] setTag:1];

-(void) HandleRadioClick
{
NSOpenPanel* dirDialog = [NSOpenPanel openPanel];
// Enable the selection of files in the dialog.
[dirDialog setCanChooseFiles:NO];

// Multiple files not allowed
[dirDialog setAllowsMultipleSelection:NO];

// Can't select a directory
[dirDialog setCanChooseDirectories:YES];


NSString *selectedFolder;
if ([dirDialog runModal] == NSOKButton)
{
    selectedFolder =[dirDialog filename];
    if([selectedFolder length] > 50)
    {
        [label setFrame:NSMakeRect(45, 120, 400, 80)];
    }
    [label setStringValue:selectedFolder];
}
else{
    [[[myMatrix cells] objectAtIndex:1] setTitle:@"Why its not deselecting" ];
    [[[myMatrix cells] objectAtIndex:1] setSelected:NO];    // Not Working
    [[[myMatrix cells] objectAtIndex:1] deselectRow:1];     // Not Working
}

}

4

1 回答 1

1
 [[[myMatrix cells] objectAtIndex:1] setSelected:NO]

 [[[myMatrix cells] objectAtIndex:1] deselectRow:1]

两者都不起作用,因为它们不是 NSButtonCell 的属性

而不是那个方法试试这个

[myMatrix selectCellAtRow:0 column:0];
于 2012-09-10T08:18:12.480 回答