Re: Deleting a row
Re: Deleting a row
- Subject: Re: Deleting a row
- From: Björn Carlström <email@hidden>
- Date: Sun, 30 May 2004 14:39:48 +0200
2004-05-30 kl. 12.18 skrev Stiphane Sudre:
On dimanche, mai 30, 2004, at 01:29 AM, Bjvrn Carlstrvm wrote:
I asked before in a different form but didnt get any response.
I want to use the delete command in the edit menu to delete a row in
an NSOutlinView. My controller object seems the ideal object to
handle this. How do I get the command sent to my controller object?
A. Do I need to sunclass NSOutlineView and add a delete method?
B. Do I make my controller a subclass of NSResponder and insert it
into the responder chain?
C Do I let my Document handle it and pass the command on to my
controller?
D. Something else?
A solution
----------
This solution means subclassing. I remember reading in several good
books on Cocoa that if my solution for a fairly common task is
subclassing, I'm probably doing something wrong. Usually the delegate
or the dataSource would handle it. Responding to the Edit menu commands
seems like something the designers of Cocoa could foresee.
But if subclassing is required I will use it. Thank you.
Implement:
- (void) deleteSelectedRowsOfOutlineView:(NSOutlineView *)
aOutlineView;
and
- (BOOL)validateMenuItem:(NSMenuItem *)aMenuItem;
in your controller
and subclass NSOutlineView:
#import <Cocoa/Cocoa.h>
@interface PBOutlineView : NSOutlineView
{
}
- (IBAction) delete:(id) sender;
@end
#import "PBOutlineView.h"
@implementation PBOutlineView
- (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();
}
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(deleteSelectedRowsOfOutlineView:)]==YES)
{
[[self dataSource]
performSelector:@selector(deleteSelectedRowsOfOutlineView:)
withObject:self];
}
}
@end
_______________________________________________
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.
_______________________________________________
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.