9
votes

In my main UIViewController I am adding a homescreen view controller as subviews:

   UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:vc];
        controller.navigationBarHidden = YES;
        controller.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
        [self addChildViewController:controller];
        [self.view insertSubview:controller.view atIndex:0];
        [controller didMoveToParentViewController:self];    

The issue is that viewDidAppear and viewWillAppear is only called once, just like viewDidLoad. Why is this? How do I make this work?

Basically inside vc I am not getting viewDidAppear nor viewWillAppear.

I also just tried adding the UIViewController without the navigation controller and it still doesn't work:

vc.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
        [self addChildViewController:vc];
        [self.view insertSubview:vc.view atIndex:0];
        [vc didMoveToParentViewController:self];    
8
Why are you using insertSubview:atIndex instead of just addSubview?ohr
it's essentialy the same thing right? I tried insertSubview and it didn't make any differencexonegirlz
Yes, essentially the same thing except addSubview adds at the end, and your insertSubview adds to the start, but that would only make a difference in the UI if there were other views already on the parent view controller.Rob
I don't understand the comment "viewDidAppear and viewWillAppear is only called once, just like viewDidLoad". Are you saying that the parent view controller is definitely getting it but the child view controllers aren't? Or are you saying that the child only gets it once (in which case I don't understand the question at all)?Rob
@RobertRyan so the parents viewDidAppear and viewWillAppear is also called once. So say I have a VC called MainViewController, which is the initial view controller set in storyboard. in my MainViewController I added the code written above (adding the child VC). Then in this child view controller (lets call it B) I am adding another child view controller, C. Basically I am expecting B's viewDidAppear to be called, when I remove C from B. Correct me I am wrong, but this is my issuexonegirlz

8 Answers

32
votes

In my case, viewDidAppear was not called, because i have made unwanted mistake in viewWillAppear method.

-(void)viewWillAppear:(BOOL)animated {
    [super viewdidAppear:animated]; // this prevented my viewDidAppear method to be called
}
14
votes

The only way I can reproduce the problem of child controllers not receiving appearance methods is if the container view controller does the following (which I'm sure you're not doing):

- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers
{
    return NO;
}

Perhaps you can try explicitly enabling this:

- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers
{
    return YES;
}

But my child view controllers definitely are getting the viewWillAppear calls (either if I explicitly automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers or if I omit this altogether.

Update:

Ok, looking at the comments under your original question, it appears that the issue is that the child controller (B) in question is, itself, a container view controller (which is perfectly acceptable) presenting another child controller (C). And this controller B's own child controller C is being removed and you're wondering why you're not getting viewWillAppear or viewDidAppear for the container controller B. Container controllers do not get these appearance methods when their children are removed (or, more accurately, since containers should remove children, not children removing themselves, when the container removes a child, it does not receive the appearance methods).

If I've misunderstood the situation, let me know.

9
votes

@Rob answer in Swift 4 (which helped me on my case which I was adding a childViewController to a UITabBarController)

override var shouldAutomaticallyForwardAppearanceMethods: Bool {
    return true
}
2
votes

Another case where this will not be called at launch time (yet may be called on when you return to the view) will be is if you have subclassed UINavigationController and your subclass overrides

-(void)viewDidAppear:(BOOL)animated

but fails to call [super viewDidAppear:animated];

1
votes

Had a same problem My container view controller did retain a child view controller via a property, but did not add a child view controller to its childViewControllers array.

My solution was to add this line of code in the container view controller

[self addChildViewController: childViewController];

After that UIKit started forwarding appearance methods to my child view controller just as expected

I also changed the property attribute from strong to weak just for beauty

1
votes

When updating my code to 13.0, I lost my viewDidAppear calls.

In Objective-c, my solution was to add the following override all to the parent master view controller.

This allowed the ViewDidAppear call to get called once again...as it did in previous IOS (12 and earlier) version.

@implementation MasterViewController

//....some methods

  • (BOOL) shouldAutomaticallyForwardAppearanceMethods { return YES; }

// ...some methods

@end

0
votes

My problem was that I was changing the tab in UITabBarController (selectedIndex = x) and then messing with the child view controllers in that tab. The problem is that it needs to be done the other way round: first mess with the child view controllers in other tab and then change the tab by setting the selectedIndex. After this change methods viewWillAppear/viewDidAppear begun to be called correctly.

-10
votes

Presenting view controllers using presentModalViewController or segues or pushViewController should fix it.

Alternatively, if for some reason you want to present your views without the built-in methods, in your own code you should be calling these methods manually. Something like this:

[self addChildViewController:controller];
BOOL animated = NO;
[controller viewWillAppear:animated];
[self.view insertSubview:controller.view atIndex:0];
[controller viewDidAppear:animated];
[controller didMoveToParentViewController:self];