如何在NSMatrix中取消选择NSButtonCell



我的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
}

}

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

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

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

代替这种方法,试试这个

[myMatrix selectCellAtRow:0 column:0];

相关内容

  • 没有找到相关文章

最新更新