Question

I have two radio buttons in my NSMatrix with mode NSRadioModeMatrix. By default the 1st Radio button is clicked. My Problem is when i click on my Second Radio Button "Let me Choose" and click on Cancel , both radio buttons seems to be selected. I have tried to deselect my Second Radio Button when "Cancel" in Selecting Folder Dialog is clicked. It works fine when a Path is selected and Open is chosen. With NSRadioModeMatrix, it must be one radio button selected at a time. But how come two buttons are selected at a time. What am i doing wrong in here

 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
}

}

Was it helpful?

Solution

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

and

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

both will not work as they are not a property of NSButtonCell

Instead of that method try this

[myMatrix selectCellAtRow:0 column:0];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top