Search code examples
iosswiftuisegmentedcontrolviewwillappearviewdidappear

viewDidAppear being called strangley


I have a viewController containing segmentedControl. I have a VCA and VCB which are in the segmentedControl. When I tap on second segment VCB appears. Now I am pushing another ViewController from VCB. But when coming back from that viewController, viewDidAppear of VCA is being Called. Which is strange to me. Because user is on the VCB so why the viewWillAppear and viewDidAppear of VCA are being called ? Here is a diagram to explain more

enter image description here

This is how I am adding viewControllers to segmentedControl

 func switchToViewController(viewController: UIViewController, selectedIndex: Int) {

    viewController.removeFromParentViewController()
    viewController.view.removeFromSuperview()

    addChildViewController(viewController)
    viewController.view.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(viewController.view)

    // Setting constraints of the container view
    NSLayoutConstraint.activate([
    viewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
        viewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
        viewController.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
        viewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
        ])

    viewController.didMove(toParentViewController: self)
}

I am just unable to understand the behavior. So please guide me.


Solution

  • You are never removing the current view controller and its view from the hierarchy...

    You need to keep track of which VC/view is currently displayed - perhaps with a currentVC variable, and your function should look something like this:

    func switchToViewController(viewController: UIViewController, selectedIndex: Int) {
    
        // remove current ViewController from VC hierarchy
        currentVC.removeFromParentViewController()
    
        // remove current VC.View from View hierarchy
        currentVC.view.removeFromSuperview()
    
        // the "incoming" ViewController becomes the "current" ViewController
        currentVC = viewController
    
        addChildViewController(viewController)
        viewController.view.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(viewController.view)
    
        // Setting constraints of the container view
        NSLayoutConstraint.activate([
        viewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
            viewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
            viewController.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
            viewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
            ])
    
        viewController.didMove(toParentViewController: self)
    }