Custom Delegate - Using _delegate works but not delegate or [self delegate]
Custom Delegate - Using _delegate works but not delegate or [self delegate]
- Subject: Custom Delegate - Using _delegate works but not delegate or [self delegate]
- From: Chris Tracewell <email@hidden>
- Date: Mon, 25 Mar 2013 12:47:58 -0700
I have a subclass of NSOutlineView that has custom delegate methods. In the implementation file I get an error for "No known instance method for selector..." when I call these declared methods using [self delegate] or delegate. However the compiler suggested using _delegate and that makes the error go away. I thought since my subclass inherits from NSOutlineView that delegate would be recognized and available.
--------------------------------------------------------------------
HEADER
--------------------------------------------------------------------
#import <Cocoa/Cocoa.h>
@class TKOutlineView; // lets compiler know the @interface deleration is coming since we put @protocol before it
// delegate protocol
@protocol TKOutlineViewDelegate <NSObject> // <NSObject> because we need to use "respondsToSelector"
@optional
-(void)outlineView:(NSOutlineView *)theOutlineView enterKeyPressedForRow:(NSInteger)theRow;
-(void)outlineView:(NSOutlineView *)theOutlineView deleteKeyPressedForRow:(NSInteger)theRow;
@end
@interface TKOutlineView : NSOutlineView {}
@end
--------------------------------------------------------------------
IMPLEMENTATION
--------------------------------------------------------------------
#import "TKOutlineView.h"
@implementation TKOutlineView
-(void)keyDown:(NSEvent *)theEvent
{
if ([[theEvent characters] length] == 0)
{
// dead key
return;
}
else if ([[theEvent characters] length] == 1 && [[theEvent characters] characterAtIndex:0] == NSEnterCharacter && [[self delegate] respondsToSelector:@selector(outlineView: enterKeyPressedForRow:)])
{
[delegate outlineView:self enterKeyPressedForRow:[self selectedRow]];
}
else if ([[theEvent characters] length] == 1 && [[theEvent characters] characterAtIndex:0] == NSDeleteCharacter && [[self delegate] respondsToSelector:@selector(outlineView: deleteKeyPressedForRow:)])
{
[delegate outlineView:self deleteKeyPressedForRow:[self selectedRow]];
}
else
{
[super keyDown:theEvent];
}
}
@end
Is _delegate the correct usage here or have I done something else wrong? Thank you.
CT
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden