BeachRunnerFred
BeachRunnerFred

Reputation: 18578

How do I write an initializer for a subclass of a view created from a nib?

I have a custom view class called TextFieldView and it's initialized from a nib...

+ (TextFieldView *)textFieldView {
    NSArray *nibObjects = [[NSBundle mainBundle]
                           loadNibNamed:@"TextFieldView"
                                  owner:nil
                                options:nil];
    return (TextFieldView *)[nibObjects objectAtIndex:0];
}

I'm trying to create a subclass called EmailTextFieldView, but I can't figure out how to write the initializer for it. My current approach is...

+ (EmailTextFieldView *)emailTextFieldView {
    return (EmailTextFieldView *)[EmailTextFieldView textFieldView];
}

This is causing a crash because anytime I create an EmailTextFieldView and call an EmailTextFieldView method on it, it crashes because the message is sent to the TextFieldView subclass, which doesn't have that method implemented. How do I write an initializer for a subclass of a view created from a nib?

Thanks so much for your wisdom!

Upvotes: 1

Views: 45

Answers (1)

J Shapiro
J Shapiro

Reputation: 3861

Your nib specifies what class it is inside of the Identity Inspector. If you are loading the TextFieldView nib, and it's set with a Custom Class of TextFieldView, you're pretty much bound to that object.

nib

If you want to create a subclass, you'll need to make another nib set to EmailTextFieldView in the Identity Inspector.

Upvotes: 1

Related Questions