Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewDidAppear not getting called

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];    
like image 410
xonegirlz Avatar asked Jul 18 '12 00:07

xonegirlz


4 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
}
like image 146
Ivan Cantarino Avatar answered Oct 12 '22 22:10

Ivan Cantarino


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
}
like image 20
luky Avatar answered Oct 12 '22 22:10

luky


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

like image 44
RichApple Avatar answered Oct 12 '22 21:10

RichApple


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

like image 26
user2905353 Avatar answered Oct 12 '22 21:10

user2905353