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.