Re: subclassing and cocoa
Re: subclassing and cocoa
- Subject: Re: subclassing and cocoa
- From: Julien Jalon <email@hidden>
- Date: Sun, 20 Apr 2003 12:37:37 +0200
On Sunday, Apr 20, 2003, at 11:59 Europe/Paris, Pejvan BEIGUI wrote:
I've written a little subclass for dropping folders on an NSImageView,
and I wanted to extend it to add some more functionalities I need, but
I'm stuck because it seems that subclassing in cocoa is really
complicated, and must have a lot of catching to do about it...
Anyway, the problems I have are the following:
1) the folderPath string remains always "null"
2) calling the setImage: method of NSImageView class doesn't change the
image if I call it from another class. while it works from inside the
class
For example, the following won't work:
testImageView = [[FFfolderWell alloc] init];
if (testImageView) {
NSLog(@"testImageView name and desc: %@ %@",[testImageView
className],
[testImageView description]);
[testImageView setImage:[WS iconForFile:@"/Users"]];
}
and I don't understand why the [testImageView super] gives me an
unknown
accessor error.
Because message "super" does really not exist for you class. Just use
"super".
My biggest concern is that if I make a category for NSImageView and
make
the pathString a global variable or an instance variable of another
class, all this code works fine.
Can anyone tell me what's wrong with this code and give me pointers or
some information about how to subclass?
Exactly as in other languages (with some exception for some Foundation
base classes wich are abstract classes with concrete private
sub-classes).
Thanks a lot for any help,
Pejvan
#import <AppKit/NSImageView.h>
@interface FFfolderWell:NSImageView {
NSMutableString * folderPath;
}
+ (id)FFfolderWell;
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender;
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender;
@end
@implementation FFfolderWell
+ (id) alloc
{
NSLog(@"FFfolderWell alloc");
self = [super alloc];
return self;
}
+ (id)FFfolderWell
{
return [[[self alloc] init] autorelease];
}
- (id) init
// initWithFrame: is the designated initializer for NSViews.. see
NSView's doc
- (id)initWithFrame:(NSRect)frameRect
{
NSLog(@"FFfolderWell init");
if (self = [super init]) {
if(self = [super initWithFrame:frameRect]) {
folderPath = [NSMutableString stringWithCapacity:64];
[folderPath retain];
// much nicer. Avoid using useless autorelease here
folderPath = [[NSMutableString alloc] initWithCapacity:64];
// or just use folderPath = @"" if you use the alternate setFolderPath:
below
return self;
}
- (void) dealloc
{
NSLog(@"FFfolderWell dealloc");
[folderPath release];
[super dealloc];
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
NSPasteboard *pboard;
NSDragOperation sourceDragMask;
sourceDragMask = [sender draggingSourceOperationMask];
pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
if (sourceDragMask & NSDragOperationCopy) {
return NSDragOperationCopy;
}
}
return NSDragOperationNone;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
NSDragOperation sourceDragMask;
NSPasteboard *pboard;
BOOL isDir;
// [self bordel];
sourceDragMask = [sender draggingSourceOperationMask];
pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
NSArray *files = [pboard
propertyListForType:NSFilenamesPboardType];
NSWorkspace * WS = [[NSWorkspace sharedWorkspace] retain];
// why are you retaining WS here?
NSString *firstFolder = [NSString stringWithString:[files
objectAtIndex:0]];
NSString *firstFolder = [files objectAtIndex:0]; // is
sufficient
if ([[NSFileManager defaultManager]
fileExistsAtPath:firstFolder
isDirectory:&isDir] && isDir) {
[self setImage:[WS iconForFile:firstFolder]];
//NSLog(@"setting folderPath: %@
(firstFolder:%@)",folderPath, firstFolder);
[self setFolderPath:firstFolder];
[self setNeedsDisplay:YES];
return YES;
}
}
//never reached
return NO;
}
//not used yet
/*
- (void) concludeDragOperation:(id <NSDraggingInfo>)sender
{
NSLog(@"concludeDragOperation");
}
*/
- (NSString *) folderPath
{
NSLog(@"FFfolderWell folderPath: %@",folderPath);
return folderPath;
}
- (void) setFolderPath:(NSString *)newPath
{
NSLog(@"newPath: %@", newPath);
[folderPath setString:newPath];
NSLog(@"newFolderPath: %@", folderPath);
}
// bad design...
// better do this
- (void)setFolderPath:(NSString *)newPath
{
if(forlderPath != newPath) {
[folderPath release];
folderPath = [newPath copy];
}
}
@end
--
Julien Jalon
http://www.julien-jalon.org/
_______________________________________________
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.