How to find user clicked on any row in NSBrowser?

1.4k views Asked by At

In my Browser implementation to select default rows I have used the following code.

[browser setTarget:self];
[browser setAction:@selector(singleClickOnBrowser:)];
[browser sendActionOn:NSLeftMouseDown];
[browser selectRow:0 inColumn:0];
[browser sendAction];

Is there any way to differentiate user click and the rows selected for the first time to select default rows?

3

There are 3 answers

0
Ram On BEST ANSWER

I did not find any API to differentiate user click and selecting the rows and columns from code. To achieve this I declared a BOOL variable to keep track of user clicks.

Following Code I changed the BOOL variable in Code.

self.userClickedOnRow = NO;
        [self.browser selectRow:0 inColumn:0];
    [self.browser sendAction];
        self.userClickedOnRow = YES;

Implemented the following delegate

 - (void)browser:(NSBrowser *)browser didChangeLastColumn:(NSInteger)oldLastColumn toColumn:(NSInteger)column
    {

        if(!self.userClickedOnRow)
        {
            NSLog(@"Programatic selection");
        }
        if (self.userClickedOnRow)  
        {
            self.userClickedOnRow = NO;
            // User Clicked on the browser,Do the required actions and set the variable again.
            self.userClickedOnRow = YES;

}
}
0
Dmitry Gaidamovitch On

You can subclass NSBrowser and override doClick. Documentation says that doClick is handler for mouse-click events, but for the moment it is also called when row is changed using keyboard arrows. The following worked for me:

@implementation MyBrowser

...

- (void)doClick:(id)sender {
    [super doClick:sender];

    //
    // Here you can add any handler AFTER the selection has changed
    //
}

...

@end
0
Chuck H On

In my app, I need to keep track of the last item selected in the NSBrowser. The following sets up the NSBrowser:

- (void)awakeFromNib
{
    [browser setDelegate:self];
    [browser setTarget:self];
    [browser setAction:@selector(browserCellSelected:)];
    [browser setSendsActionOnArrowKeys:YES];
}

The following handles cells as they get selected. This works with selections made by the mouse or with the keyboard. If your app allows multiple selections, your action method will need to handle that.

- (void)browserCellSelected:(id)sender
{
    NSIndexPath *indexPath = [browser selectionIndexPath];
    MyItem *myItem = [browser itemAtIndexPath:indexPath];
    if (myItem)
    {
        NSLog(@"Selected Item: %@", myItem.name);
    }
}

BTW, programmatic selections will not fire the browserCellSelected: method and user clicks will not call the delegate's browser:selectRow:inColumn: method.