Search code examples
objective-cmacoscocoanstextfield

Default values for custom cocoa object


There are a few parameters that I always set when creating MyLabel and rather than having to write them out everytime I would rather just set them as default for the custom class.

I have tried this with no luck:

@interface MyLabel: NSTextField
@end

@implementation MyLabel
-(id)init {
    if (self = [super init]) {
        [self setWantsLayer:YES];
        [self setSelectable:YES];
        [self setEditable:NO];
        [self setBordered:NO];
    }
    return self;
}
@end

init is just not called.

MyLabel is called like:

MyLabel* error_label = [[MyLabel alloc] initWithFrame: ...

Solution

  • Do not use initWithFrame. Call only with init.

    Try below code

    MyLabel* error_label = [[MyLabel alloc] init];
    

    You can set frame after initialization like below

    [error_label setFrame::CGRectMake(0, 0, 0, 0)]; // set co-ordinates accordingly