1

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,

John smith
  • 355
  • 4
  • 17
  • use arc don't dealloc manually .. – vaibhav Sep 20 '16 at 12:25
  • I don't want to dealloc manually, I wan't to perform an action when before the object is released .. – John smith Sep 20 '16 at 12:39
  • How about performing your methods using willAppear and willDisappear on your customViewController? You could also check didMoveToParentViewController – Teffi Sep 20 '16 at 13:00
  • I agree with @Teffi that probably you may just use viewWillAppear/Disappear in most cases. I usually load static content (initializing, formatting and connecting views) in the viewDidLoad and dynamic content (data initialization) in the viewWillAppear if necessary. – jalone Sep 20 '16 at 13:25

1 Answers1

1

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.

jalone
  • 1,953
  • 4
  • 27
  • 46