tags:

views:

3069

answers:

2

I am trying to subclass NSOutlineView. Here is my code:

OutlineViewSublcass.h:

#import <Cocoa/Cocoa.h>

@interface OutlineViewSubclass : NSOutlineView {

}

@end

OutlineViewSubclass.m:

#import "OutlineViewSubclass.h"

@implementation OutlineViewSubclass

- (id)initWithFrame:(NSRect)frame
{
 self = [super initWithFrame:frame];
 printf("debug A\n");
 return self;
}

- (void)awakeFromNib
{
 printf("debug B\n");
}

@end

The debug output is:

debug B

Why isn't (id)initWithFrame:(NSRect)frame being called?

+15  A: 

Cocoa controls implement the NSCoding protocol for unarchiving from a nib. Instead of initializing the object using initWithFrame: and then setting the attributes, the initWithCoder: method takes responsibility for setting up the control when it's loaded using the serialized attributes configured by Interface Builder. This works pretty much the same way any object is serialized using NSCoding.

It's a little bit different if you stick a custom NSView subclass in a nib that doesn't implement NSCoding, in that case initWithFrame: will be called. In both cases awakeFromNib will be called after the object is loaded, and is usually a pretty good place to perform additional initialization in your subclasses.

Marc Charbonneau
+4  A: 

Official Apple answer for this is here.

Doug Richardson