UIViewController viewWillAppear not called when adding as subView

前端 未结 8 1401
野性不改
野性不改 2020-12-14 06:43

I have a UIViewController that I am loading from inside another view controller and then adding its view to a UIScrollView.

self.st         


        
相关标签:
8条回答
  • 2020-12-14 07:07

    I encounter -viewWillAppear: not called problem again. After googling, I came here. I did some tests, and find out that the calling order of -addSubview and -addChildViewController: is important.

    Case 1. will trigger -viewWillAppear: of controller, but Case 2, it WON'T call -viewWillAppear:.

    Case 1:

      controller?.willMoveToParentViewController(self)
    
      // Call addSubview first
      self.scrollView.addSubview(controller!.view)
      self.addChildViewController(controller!)
    
      controller!.didMoveToParentViewController(self)
    

    Case 2:

      controller?.willMoveToParentViewController(self)
    
      // Call adChildViewController first      
      self.addChildViewController(controller!)      
      self.scrollView.addSubview(controller!.view)
    
      controller!.didMoveToParentViewController(self)
    
    0 讨论(0)
  • 2020-12-14 07:09

    Check if your parent VC is a UINavigationViewController (or any other container). In this case the shouldAutomaticallyForwardAppearanceMethods is False and the appearance methods are not called.

    0 讨论(0)
  • 2020-12-14 07:11

    The previous answers are correct, but in case it helps someone - if you override loadView in the child view controller, then none of the other UIViewController methods get called.

    Took me some time to realize why my code wasn't running properly, until I realized that I had accidentally overridden loadView instead of viewDidLoad.

    0 讨论(0)
  • 2020-12-14 07:12

    As mentioned in another answer, the parent view controller might not call viewWillAppear etc. when shouldAutomaticallyForwardAppearanceMethods is set to false. UINavigationController and UITabBarController are known to do that. In this case, you need to call beginAppearanceTransition(_ isAppearing: Bool, animated: Bool) on the child view controller with isAppearing set to true when the view appears and vice versa.

    You have to place these calls at appropriate places in your code, normally when you add and remove your child view controller.

    Don't forget to call endAppearanceTransition on your child view controller when your custom transition has ended, otherwise viewDidAppear and viewDidDisappear are not called.

    0 讨论(0)
  • 2020-12-14 07:20

    You should add statisticsController as a child view controller of the controller whose view you're adding it to.

    self.statisticsController = [self.storyboard instantiateViewControllerWithIdentifier:@"StatisticsViewController"];
    self.statisticsController.match = self.match;
    
    [self.scrollView addSubview:self.statisticsController.view];
    [self addChildViewController:self.statisticsController];
    [self.statisticsController didMoveToParentViewController:self];
    

    I'm not sure this will make viewDidAppear get called, but you can override didMoveToParentViewController: in the child controller, and that will be called, so you can put any code that you would have put in viewDidAppear in there.

    0 讨论(0)
  • 2020-12-14 07:23

    Per Apple (https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html), the correct order of API calls to add a child view controller is:

    [self addChildViewController:childVC];
    [self.view addSubview:childVC.view];
    [childVC didMoveToParentViewController:self];
    

    But I still had the problem where viewWillAppear in the child VC was not sporadically getting called. My issue was that there was a race condition that could cause the code above to get executed before viewDidAppear in the container view controller was called. Ensuring that viewDidAppear had already been called (or deferring the addition of the child VC until it was) solved it for me.

    0 讨论(0)
提交回复
热议问题