6

When am I supposed to use the initWithCoder: method?

2 Answers 2

12

Yes, if you are using a custom class in IB, then those objects are instantiated with the initWithCode: method. So, in your class you would override:

-(id) initWithCoder:(NSCoder*)aDecoder {
    if (! (self = [super initWithCoder:aDecoder]))
        return nil;

    // object has been created from IB... do initialization stuff here

    return self;
}
Sign up to request clarification or add additional context in comments.

Comments

5

You are supposed to use the initWithCoder: method when you are working with objects that have been archived. For example when you specifically use NSKeyedUnarchiver to create these archived objects or when you need to add custom initialization code to objects that are coming from a xib file.

1 Comment

the reason I ask is because i've created a subclass of UIScrollView, i'd like to set it position in the xib and grab that info and use it to set up the subclass, would I use initWithCoder: for something like that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.