0

I'm trying to use self.delegate in a custom UICollectionView subclass. However, when trying to use self.delegate in initWithCoder the value is still nil. It does change at a later stage, for instance when in controller's viewDidLoad or view's awakeFromNib.

When in the View/Controller lifecycle does this value gets set?

Where is the first place I can use it safely?

Xyand
  • 4,470
  • 4
  • 36
  • 63
  • 1
    See [the first answer here](http://stackoverflow.com/questions/15508041/should-i-be-using-awakefromnib-or-initwithcoder-here). I would just use viewDidLoad in this case. – p4sh4 Jun 29 '14 at 05:55
  • @p4sh4, In my case I wish to separate this logic from the controller. – Xyand Jun 29 '14 at 06:02

2 Answers2

2

awakeFromNib is the point you're looking for. That's the first point in the instantiation from a nib file that you're guaranteed that subviews and outlets have been configured.

David Berry
  • 40,941
  • 12
  • 84
  • 95
1

When you create a view controller in a storyboard, the attributes you configure in Interface Builder are serialized into an archive. Later, when the view controller is instantiated, this archive is loaded into memory and processed. The result is a set of objects whose attributes match those you set in Interface Builder. The archive is loaded by calling the view controller’s initWithCoder: method.

Then, the awakeFromNib method is called on any object that implements that method, awakeFromNib gets called after the view and its subviews were allocated and initialized. It is guaranteed that the view will have all its outlet instance variables set. You use this method to perform any configuration steps that require other objects to already be instantiated. For detail refer ViewController programming guide

Yatheesha
  • 10,412
  • 5
  • 42
  • 45