1
votes

I see that when view controller is loaded from storyboard, these events happen in order

  • awakeAfterUsingCoder
  • awakeFromNib
  • viewDidLoad

In awakeFromNib, I see that isViewLoaded == false. From Which should I use, -awakeFromNib or -viewDidLoad?

awakeFromNib is called when the controller itself is unarchived from a nib. viewDidLoad is called when the view is created/unarchived. This distinction is especially important when the controller's view is stored in a separate nib file.

In the spec of awakeFromNib

The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet and action connections already established.

So which outlet and action does it mention? The other thing is that awakeAfterUsingCoder is called 3 times in my case !!!

1

1 Answers

0
votes

This only works with nibs. As I understand if we're loading ViewControllers from storyboard awakeFromNib gets called but before the view and subviews are initialised. That's why there is no guarantee that the view and outlets will be initialised. So if you need the object with established outlet and action connections you need to start awakeFromNib with [self view] it`s like a little trick that helps.