iOS routing viewDidAppear to child view controllers?
Asked Answered
D

7

9

I'm adding a child view controller to a parent view controller, and everything works as expected, except that child view controller isn't having its usual callbacks triggered. eg, things like viewWillAppear(animated) is never called. I looked into it and thought it may be because I wasn't calling willMoveToParentViewController and didMoveToParentViewController on the child view controller before and after adding it to the parent, respectively. Unfortunately, fixing that has made no difference. Does anyone know how I can wire this up so that when I do addChildViewController and removeChildViewController the regular callbacks are triggered? After adding the childViewController, I also add its view as a subview to the parent view controller's view. At neither point (addChildViewController & addSubview) does the child view controller's viewWillAppear(animated), etc methods get called...

Nick

Daysidayspring answered 24/12, 2013 at 2:15 Comment(0)
G
12

viewWillAppear, viewDIdAppear called automatically when you adding your ViewControler view to a view hierarchy

viewWillDisappear, viewDidDisappear called automatically when you removing your ViewControler view from a view hierarchy

Maybe this methods not called because you are adding sub-controller view before displaying main view controller and, therefore your main view is not in view hiererchy itself?

When your main view controller appears or disappears you should call this methods for childs in corresponding methods.

Gies answered 25/12, 2013 at 5:38 Comment(0)
B
8

Not sure if this can apply to your situation, but try experimenting with manually sending appearance callback methods to your children.

From apple's view controller containment documentation:

However, sometimes the default behavior may send those events in an order that doesn’t make sense for your container. For example, if multiple children are simultaneously changing their view state, you may want to consolidate the changes so that the appearance callbacks all happen at the same time in a more logical order. To do this, you modify your container class to take over responsibility for appearance or rotation callbacks.

It recommends manually forwarding the appearance callbacks to your children if you want more fine grained control:

// From the container view controller
- (BOOL) shouldAutomaticallyForwardAppearanceMethods
{
    return NO;
}

-(void) viewWillAppear:(BOOL)animated
{
    [self.child beginAppearanceTransition: YES animated: animated];
}

-(void) viewDidAppear:(BOOL)animated
{
    [self.child endAppearanceTransition];
}

-(void) viewWillDisappear:(BOOL)animated
{
    [self.child beginAppearanceTransition: NO animated: animated];
}

-(void) viewDidDisappear:(BOOL)animated
{
    [self.child endAppearanceTransition];
}
Blowout answered 4/3, 2015 at 16:12 Comment(0)
A
8

Try this one:

childController.willMoveToParentViewController(self)
childController.beginAppearanceTransition(true, animated: true)

after added to your view controller

Assonance answered 3/9, 2015 at 9:3 Comment(1)
Make sure to call childController.endAppearanceTransition() after your animation transition ends, otherwise the viewDidAppear won't get invoked!Squier
A
4

Just tried do the following in viewDidLoad in the parent viewcontroller and it seems to work

ChildExperimentViewController *child = [[ChildExperimentViewController alloc]init];
[self addChildViewController:child];
[self.view addSubview:child.view];
[child didMoveToParentViewController:self];
Athens answered 24/12, 2013 at 2:33 Comment(0)
E
0
let vc = ChildVC.init()
vc.frame = self.view.bounds
self.addChildViewController(vc)
self.view.addSubview(vc.view)
vc.willMove(toParentViewController: self)
vc.didMove(toParentViewController: self)
Exmoor answered 11/9, 2017 at 9:32 Comment(0)
E
0

Chourobin answer in Swift:

override var shouldAutomaticallyForwardAppearanceMethods: Bool {
    return false
}

override func viewWillAppear(_ animated: Bool) {
    self.childViewControllers[self.segmentView.selectedSegmentIndex].beginAppearanceTransition(true, animated: animated)
}

override func viewDidAppear(_ animated: Bool) {
    self.childViewControllers[self.segmentView.selectedSegmentIndex].endAppearanceTransition()
}

override func viewWillDisappear(_ animated: Bool) {
    self.childViewControllers[self.segmentView.selectedSegmentIndex].beginAppearanceTransition(false, animated: animated)
}

override func viewDidDisappear(_ animated: Bool) {
    self.childViewControllers[self.segmentView.selectedSegmentIndex].endAppearanceTransition()
}

P.S. As I am not keeping the reference to a child VC as a property or a variable. I preferring to access it using the currently selected segment index. SegmentView is a third party UISegmentControl.

Exmoor answered 11/9, 2017 at 9:46 Comment(0)
B
-4

You can manual call the child view controller's viewWillAppear in parectViewController,code like this

-(void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    [selectedViewController viewWillAppear:animated];
}

if you have add a navigationController as childViewController you can add code like this in navigationController's delegate

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{
    [viewController viewWillAppear:animated];
}

if you have add a tabBarController as childViewController you can add code like this in tabBarController's delegate and super view

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    [self.tabBarController.selectedViewController viewWillAppear:animated]; 
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 
    [viewController viewWillAppear:NO]; 
}

I don't know if this can solve your problem,hope be able to help you.

Bonzer answered 24/12, 2013 at 2:53 Comment(1)
We should never call viewWillAppear or other methods manually.. please refer developer.apple.com/library/ios/documentation/UIKit/Reference/…:Kilpatrick

© 2022 - 2024 — McMap. All rights reserved.