2012-09-10 28 views
1

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

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 
} 

}

回答

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

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

都将无法正常工作,因为他们不是NSButtonCell

的属性

取而代之的是方法试试这个

[myMatrix selectCellAtRow:0 column:0]; 
+0

刚刚尝试过这个vignesh,但是你给的东西也不起作用。 (0,0)处的单元格没有与上面的行一起选择。 – Zeus

+0

你的意思是从另一个函数改变它,而这个函数将从矩阵动作方法调用。让我试着更新你 – Zeus

+0

@Zeus我试图改变选择,即使在 - (void)windowDidBecomeKey:(NSNotification *)通知委托方法,但选择无法更改。 –

相关问题