简体   繁体   中英

viewDidload and viewDidAppear not being called?

I have 3 view controllers and a container view controller. I add those 3 view controllers as child view controllers in the container. When I launch the app I have print statements in viewDidload and viewDidAppear in all the view controllers which get executed.

The problem is: When I "scroll" back on those views and it "appears" again the print statements do not execute nor any code inside viewDidAppear nor viewDidLoad. Why is this happening?

Here is my code where I instantiate my view controllers. Thanks for the help!

override func viewDidLoad() {
    super.viewDidLoad()

    scrollView.delegate = self



    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    page1 = storyboard.instantiateViewController(withIdentifier: StoryboardIdentifiers.feedViewController.rawValue) as! FeedViewController
    page1.view.translatesAutoresizingMaskIntoConstraints = false
    page1.delegate = self
    scrollView.addSubview(page1.view)
    addChildViewController(page1)
    page1.didMove(toParentViewController: self)

    page2 = storyboard.instantiateViewController(withIdentifier: StoryboardIdentifiers.favoritesViewController.rawValue) as! FavoritesViewController
    page2.view.translatesAutoresizingMaskIntoConstraints = false
    page2.delegate = self
    scrollView.addSubview(page2.view)
    addChildViewController(page2)
    page2.didMove(toParentViewController: self)

    page3 = storyboard.instantiateViewController(withIdentifier: StoryboardIdentifiers.settingsViewController.rawValue) as! SettingsViewController
    page3.view.translatesAutoresizingMaskIntoConstraints = false
    scrollView.addSubview(page3.view)
    addChildViewController(page3)
    page3.didMove(toParentViewController: self)


}

What would trigger those view controllers to cycle through their life cycles?

The parent view controller, whatever it's named will run through it's load cycle. All views will also run through their. (I bet if you subclass a view and, using it, put a breakpoint inside draw(rect:) it would hit it.) But there's nothing to trigger the child view controllers to do their thing.

From what I see, your view hierarchy is:

  • Main view
  • Scroll view inside main view
  • Three page views inside scroll view

When the main view (the afore-mentioned parent view controller) loads, it goes through the usual viewDidLoad , viewWillAppear , viewWillLoadSubviews .... and it's there that all of the above views get loaded, laid out, and drawn.

Nothing is happening to trigger those three page controllers to do any loading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM