Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is view initialized when loaded via a storyboard?

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?

like image 442
James Raitsev Avatar asked Dec 04 '11 04:12

James Raitsev


1 Answers

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.

like image 80
Caleb Avatar answered Sep 28 '22 02:09

Caleb