How is view initialized when loaded via a storyboard?

Objective CIosxcode4.2Storyboard

Objective C Problem Overview


When view is loaded manually, developer remains in control when it comes to initializations, we choose what initializer to call, what variables to set etc.

When view is loaded from the storyboard segue ... what happens to that initializer? Where should variables be set i'd like to be available once view had been loaded?

Please help me understand the sequence here. How is instance of the class created here, who creates it and how can we intervene and help set it up to our liking?

Objective C Solutions


Solution 1 - Objective C

When a view is loaded from a nib or storyboard, it's -initWithCoder: method is called. Like -initWithFrame:, -initWithCoder: is a designated initializer for UIView. If you're going to do any custom initialization for a UIView subclass, you should make sure that it happens for both these methods. One common technique is to add a common initialization method that you call from both -initWithFrame: and -initWithCoder:. See my answer to Custom view and implementing init method? for a more detailed description.

Note that the documentation for -initWithFrame: explains:

> If you use Interface Builder to design your interface, this method is > not called when your view objects are subsequently loaded from the nib > file. Objects in a nib file are reconstituted and then initialized > using their initWithCoder: method, which modifies the attributes of > the view to match the attributes stored in the nib file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames RaitsevView Question on Stackoverflow
Solution 1 - Objective CCalebView Answer on Stackoverflow