Re: Grabbing key presses in NSTableView
Re: Grabbing key presses in NSTableView
- Subject: Re: Grabbing key presses in NSTableView
- From: Stéphane Sudre <email@hidden>
- Date: Tue, 27 Apr 2004 19:46:30 +0200
On mardi, avril 27, 2004, at 06:28 PM, Bill Cheeseman wrote:
on 2004-04-27 11:29 AM, matt neuburg at email@hidden wrote:
On Wed, 21 Apr 2004 09:52:04 +0200, "Kasper J. Jeppesen"
<email@hidden> said:
I need to let my user delete records from a tableview simply by
pressing backspace. The only clues i have been able to find, towards
doing this, involves subclassing NSTableView. Is that really the only
way?
Lots of applications do this, so I would almost asume cocoa has some
really neat way of doing it (like so many other things in cocoa)...
any
clues?
That's how I do it - subclass NSTableView and implement keyDown: (and
notify
the delegate). Why is this approach undesirable or non-"neat"? m.
I do it differently -- but I don't have my code handy, so I'll have to
describe my technique from memory. I consider my technique "neater"
only
because it makes good use of IB and requires less writing of code.
1. Add a button to your window in IB (and hide it if you don't want to
have
a button separate from the table, although I think the usual "+" and
"-"
buttons adjacent to table views constitute good design).
2. Assign the Delete key equiv. to the button in IB.
3. Write a -deleteRow: action method, and connect it to the button in
IB.
4. Write the -deleteRow: action method so that it removes the selected
row's
array element from the table view's datasource, and update the table
view.
5. Press Delete and watch the row disappear!
Alternatively (or in addition), add a menu item to delete a row. Just
use IB
to add the menu item and assign it the Delete key equiv., then connect
the
same action method to the menu item.
In general, whenever I'm looking to accomplish something with a
keystroke, I
think about key equivalents for menu items and buttons before I think
about
subclassing a view and implementing -keyDown:.
I think the subclass solution is better for the following reasons:
1) You don't have to have a delete button even visible or invisible.
And you don't need to disable it when there is no selection.
2) You don't have to set the delete key equivalent to the delete menu
item which does not conform to the HIG. And one point you're forgetting
is that the delete item needs to be disabled when there is no selection.
Here is some code for a NSTableView subclass:
- (void) keyDown:(NSEvent *) theEvent
{
NSString * tString;
unsigned int stringLength;
unsigned int i;
unichar tChar;
tString= [theEvent characters];
stringLength=[tString length];
for(i=0;i<stringLength;i++)
{
tChar=[tString characterAtIndex:i];
if (tChar==0x7F)
{
NSMenuItem * tMenuItem;
tMenuItem=[[NSMenuItem alloc] initWithTitle:@""
action:@selector(delete:) keyEquivalent:@""];
if ([self validateMenuItem:tMenuItem]==YES)
{
[self delete:nil];
}
else
{
NSBeep();
}
[tMenuItem release];
return;
}
}
[super keyDown:theEvent];
}
- (BOOL)validateMenuItem:(NSMenuItem *)aMenuItem
{
if ([aMenuItem action]==@selector(delete:))
{
if ([self numberOfSelectedRows]>0)
{
return [[self dataSource] validateMenuItem:aMenuItem];
}
return NO;
}
return YES;
}
- (IBAction) delete:(id) sender
{
if ([[self dataSource]
respondsToSelector:@selector(deleteSelectedRowsOfTableView:)]==YES)
{
[[self dataSource]
performSelector:@selector(deleteSelectedRowsOfTableView:)
withObject:self];
}
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.