Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we have to call func didMove(toParent parent: UIViewController?) after calling addChild(UIViewController)?

Tags:

What exactly is a purpose of calling func didMove(toParent parent: UIViewController?)?

From apple documentation it says:

If you are implementing your own container view controller, it must call the didMove(toParent:) method of the child view controller after the transition to the new controller is complete or, if there is no transition, immediately after calling the addChild(_:) method.

The removeFromParent() method automatically calls the didMove(toParent:) method of the child view controller after it removes the child.

It seems like the function updates the transition of a viewcontroller, but I can't fully grasp the idea behind calling it. What would happen if I don't call it?

like image 226
Hitit Avatar asked Jul 12 '19 06:07

Hitit


People also ask

What is UIViewController in iOS?

A UIViewController is an object which manages the view hierarchy of the UIKit application. The UIViewController defines the shared behavior and properties for all types of ViewController that are used in the iOS application. The UIViewController class inherits the UIResponder class.

What are view controllers in Swift?

You use view controllers to manage your UIKit app's interface. A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed.


1 Answers

If you don't call it the child view controller won't be able to detect that it did move to the parent view controller. This may be important. Sometimes you need to do something in the child controller exactly after moving to the parent

like image 159
Andrey Chernukha Avatar answered Sep 28 '22 12:09

Andrey Chernukha