Do I programmatically add SubViews in ViewDidAppear, ViewDidLoad, ViewWillAppear, the constructor?

前端 未结 3 928
無奈伤痛
無奈伤痛 2020-12-22 18:21

I\'m trying to figure out from Apple\'s sketchy documentation which method is the best place to be initializing and adding my Views controls to the controller\'s view.

3条回答
  •  有刺的猬
    2020-12-22 19:20

    Hmm, Apple's docs seem to be pretty clear, IMHO.

    If you create your own root view (the root view of this particular controller's view hierarchy) programmatically, you should create it in -loadView without calling super and set the view property when done. If your view is loaded from a nib, you should not touch -loadView.

    You add custom subviews to the view controller's view or otherwise modify it in -viewDidLoad. The recommended practice is to create your UILabel and UIWebView in -viewDidLoad and release them in -viewDidUnload, setting their references to nil if you need to keep them in ivars.

    Note: -viewDidUnload is deprecated in iOS 6 and is just not called any more, because UIViewController no longer purges its view under memory pressure.

提交回复
热议问题