UIViewController viewWillAppear not called when adding as subView

前端 未结 8 1418
野性不改
野性不改 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)
    

提交回复
热议问题