Description of initWithNibName, awakeFromNib and viewDidLoad?

后端 未结 3 951
星月不相逢
星月不相逢 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:26

    I just finished doing some research on this topic so I thought I'd share a few of the things I learned.

    1. There's nothing wrong with using awakeFromNib with views for the iPhone. See this Apple Dev document.

    2. initWithCoder is not a good place to do initialization when the view is loaded from a NIB file because other items in the same NIB file may or may not have been initialized at that point. So an outlet might still be nil, for example. All items in the same NIB file are guaranteed to be initialized properly when awakeFromNib is called.

    3. viewDidLoad is a good place to do setup work in the viewController.

    So why would one want to use awakeFromNib in the view? One reason I can think of is if you have stuff you want to do after the view has been initialized and hooked up to the other objects in the NIB file, but you want to encapsulate it in the view only. This reduces linkage with the view controller.

提交回复
热议问题