iOS: What is the difference between -init and -viewLoad of a ViewController?

前端 未结 3 1620
遥遥无期
遥遥无期 2020-12-05 10:35

I don\'t know exactly what\'s the right place to set things like the tintColor of the NavigationBar or the title of my ViewController. It works in the -init method and the -

相关标签:
3条回答
  • 2020-12-05 11:09

    The init methods (yes there is more then one) are where the UIViewController is initialized. Thus this is the place where you do stuff for the UIViewController and not its views.

    If you use a nib to load you view then the best place to set any properties is the viewDidLoad method. This method gets called after the nib is loaded. If you set up the view programatically use the loadView method then this is the place to set UIControl properties.

    Since the system can unload views to save memory, it will leave the UIViewController alone. Any properties set in the init methode will not be applied again, since the UIViewController is already initialized.

    0 讨论(0)
  • 2020-12-05 11:16

    The right place is to set it in viewDidLoad. To know more about those methods, apple has provided the documentation

    0 讨论(0)
  • 2020-12-05 11:20

    the init method is used to initialize the viewController while viewDidLoad method is used to load your nib(i.e. your view). so when you want to do something with your viewController then use init method and when you want to do something with your view then use viewDidLoad.

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