Is it possible to make my NSTableView accept a deleteevnt (backspace og even cmd+backspace) ? I have an NSMenu where i have my delete-menu-item connected to my first responder object in the nib.
Any pointers?
Is it possible to make my NSTableView accept a deleteevnt (backspace og even cmd+backspace) ? I have an NSMenu where i have my delete-menu-item connected to my first responder object in the nib.
Any pointers?
You could create a subclass of NSTableView, overriding keyDown
like so:
- (void)keyDown:(NSEvent *)theEvent
{
unichar key = [[theEvent charactersIgnoringModifiers] characterAtIndex:0];
if(key == NSDeleteCharacter)
{
[self deleteItem];
return;
}
[super keyDown:theEvent];
}
Then make sure that any NSTableView that you want to have the delete functionality uses your subclass in Interface Builder instead of the regular NSTableView.
You can implement the - (void)deleteItem
method for example like this:
- (void)deleteItem
{
if ([self numberOfSelectedRows] == 0) return;
NSUInteger index = [self selectedRow];
[documentController deleteItemWithIndex:index];
}
This is a modern solution using NSViewController
and First Responder
.
The Delete
menu item in the menu Edit
is connected to the selector delete:
of First Responder. If there is no Delete
menu item, create one and connect it to delete:
of First Responder (red cube).
Delete
menu item (⌫ or ⌘⌫)In the view controller implement the IBAction
method
Swift: @IBAction func delete(_ sender: AnyObject)
Objective-C: -(IBAction)delete:(id)sender
and put in the logic to delete the table view row(s).
No subclass needed.
One approach which is easy to implement:
When you build your project, given that you implement the deleteRecord method, a Backspace keypress will delete records from your tableview
The correct way of implementing this functionality is by using key-bindings:
Depending upon which kind of application you write, there are validation delegate methods. By which means you can set the menu items enabled state. For a document based application this validation happens through - (BOOL)validateUserInterfaceItem:(id)anItem
.
You could create a subclass of NSTableView, overriding keyDown
like so:
- (void)keyDown:(NSEvent *)theEvent
{
unichar key = [[theEvent charactersIgnoringModifiers] characterAtIndex:0];
if(key == NSDeleteCharacter)
{
[self deleteItem];
return;
}
[super keyDown:theEvent];
}
Then make sure that any NSTableView that you want to have the delete functionality uses your subclass in Interface Builder instead of the regular NSTableView.
You can implement the - (void)deleteItem
method for example like this:
- (void)deleteItem
{
if ([self numberOfSelectedRows] == 0) return;
NSUInteger index = [self selectedRow];
[documentController deleteItemWithIndex:index];
}
keyDown:
method. –
Majordomo © 2022 - 2024 — McMap. All rights reserved.
keyDown:
method. – Majordomo