Reputation: 536
In storyboards, I setup ParentViewController to embed ChildViewController. I ran print statements in both of their viewDidLoad()
methods, and my child view controller's view is loaded before the parent. I do not access the child's view in prepare(for:)
.
Does anybody know why the child's view is loaded before the parent's? I would think since storyboards/segues automatically call parentView.addSubview(childView)
that that would mean the parent's view is loaded first so we can call addSubview
.
Upvotes: 1
Views: 1690
Reputation: 12385
What you are forgetting is loadView()
, the lifecycle method that comes before viewDidLoad()
. loadView()
is responsible for view and subview construction and if you print in this method you'll find that the parent's loadView()
is called before the child's, and that is because the parent's view must be constructed before it can add subviews, like a child's view (you're correct). loadView()
is also where the parent-child relationship is established (in the parent's loadView()
) and the parent must get its children to viewDidLoad()
before getting itself to viewDidLoad()
because the child's view is (in effect) the parent's view. The parent has no view, in some sense, it only displays its children's views. Therefore, the parent must get its children to viewDidLoad()
before it can claim its own view did load.
Upvotes: 4