Description of initWithNibName, awakeFromNib and viewDidLoad?

后端 未结 3 947
星月不相逢
星月不相逢 2020-12-12 13:46

Is there a good overview of initWithNibName, awakeFromNib, and viewDidLoad that lays out the best way to use each of these and describ

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 14:47

    viewDidLoad can get called more than once so it is not an appropriate place for most initialization (although in practice it's called just once unless you are meddling) - that is why awakeFromNib is special and very necessary: it only gets called once.

    viewDidLoad doesn't exist on UIView - UIView has awakeFromNib for initialization and there are no other sensible options.

    For the past six months I have been happily using awakeFromNib with storyboards (a single storyboard for an app) without any trouble at all and it has behaved perfectly with all outlets configured as specified in the documentation. Then today I took some code apart and put in a new view controller and views and none of it is working as I have experienced in the past i.e. all outlets nil in awakeFromNib. My theory is simple - the SDK sucks because it's very easy to do something trivial somewhere in your chain of controllers/views and classes and break everything.

    Now I'm off to research this pointless topic, yet again, when all I need is a simple consistent place to do initialization.

提交回复
热议问题