viewDidAppear not getting called

Viewed 28005

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 Answers

@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
}

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];

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

Related