John smith
John smith

Reputation: 353

Objective c handle adding child view controller or remove from parent

I have a small question about UIViewController.

Is it possible to handle, in a custom UIViewController class, when the controller is added to current view controller using

[self addChildViewController:customViewController];

or when removing

[customViewController removeFromParentViewController];

For now, I have done what I want using viewDidLoad and dealloc methods but I was wondering if there were a better solution.

Thanks,

Upvotes: 1

Views: 915

Answers (1)

jalone
jalone

Reputation: 2073

Your view controller can override this method when it wants to react to being added to a container.

- (void)didMoveToParentViewController:(UIViewController *)parent;

As by Apple doc

If you are implementing your own container view controller, it must call the didMoveToParentViewController: 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 addChildViewController: method.

The correspectiv for dealloc (which is anyway discouraged) is by Apple doc

- (void)willMoveToParentViewController:(UIViewController *)parent;

If you are implementing your own container view controller, it must call the willMoveToParentViewController: method of the child view controller before calling the removeFromParentViewController method, passing in a parent value of nil.

Upvotes: 1

Related Questions