views:

277

answers:

2

I have a custom class that has quite a few accessor methods for customizing it. My question is, if a programmer uses this class and doesn't bother to assign values for ALL of these methods because he doesn't know about them or doesn't want to bother, how can I make the class assume a default view? I can't use awakeFromNib, because that would override purposeful customization in, say, an AppController awakeFromNib.

Any simple way to do this?

EDIT:

The class in an NSView, and the customization methods just modify an instance variable then tell the view to redraw (background color, etc.). When I don't explicitly tell the object somewhere to assign values for ALL of these customizations, it sets them to zero. I need to change this to where they assume usable default values.

+1  A: 

I'm confused about what you're trying to do. If you want to set default values for class members, just assign to them in your init method(s):

- (id) init
{
    if((self = [super init]))
    {
        member1 = member1default;
        member2 = member2default;  // etc.
    }
    return self;
}

- (id) initWithCoder:(NSCoder *)encoder  // this is called for objects constructed from a NIB
{
    if((self = [super initWithCoder:encoder]))
    {
        member1 = member1default;
        member2 = member2default;  // etc.
    }
    return self;
}
Adam Rosenfield
It is an NSView, so would I override initWithFrame? I tried this, implementing an init like: - (id)initWithFrame:(NSRect)frameRect { } Then doing basically what you're doing above, assigning values to instance variables. The app crashes at startup, saying "loading largenumber of stack frames, x%."
The view is placed in a NIB file, fyi.
A: 

(Sorry for being a StackOverflow noob; I can't seem to comment on other people's answers yet.)

Yes, you typically override initWithFrame in a custom NSView subclass. Make sure you're calling the superclass's initWithFrame: method as well:

self = [super initWithFrame:frameRect];

The error message about loading lots of stack frames makes me think you may have inadvertently created an infinite recursive loop. However, without seeing the code I couldn't say for sure.

Ryan Ballantyne