initWithNibName VS viewDidLoad

后端 未结 3 575
小鲜肉
小鲜肉 2020-12-31 20:01

I\'ve been trying to understand for hours when I should use the viewDidload: and when I should use initWithNibName: to set up the properties of my viewController.

F

相关标签:
3条回答
  • 2020-12-31 20:03

    You should use viewDidLoad: method of your controller. To quote from Apple's documentation on initWithNib:

    The nib file you specify is not loaded right away. It is loaded the first time the view controller’s view is accessed. If you want to perform additional initialization after the nib file is loaded, override the viewDidLoad method and perform your tasks there.

    0 讨论(0)
  • 2020-12-31 20:18

    You should set up your properties in the viewDidLoad. This method is called by the system when the controller's view is loaded into memory. The initWithNibName: is something that you call when you create a controller instance from a nib file.

    That is to say, that if you set up your properties in the initWithNibName: and instead you call init, your controller might not be in a good state; thus, it's best to do in viewDidLoad.

    0 讨论(0)
  • 2020-12-31 20:28

    initWithNibName: is called when the NIB is loaded and instantiated.

    viewDidLoad: is called when your view is actually presented onscreen.

    And yes - I believe that in your case, setting colors and such are best done in initWithNibName

    0 讨论(0)
提交回复
热议问题