title: dimiss modal and return to presented childViewController in containerView
body: <p>I am having a bit of an issue with dismissing a modal view presented from a childviewController in a container view. I have a UINavigationController as the rootViewController (MainNavigationController), and present a modal from one of the childViewControllers from the selectedSegmentIndex 1 (secondViewController). The modal is presented fine, but when I dismiss the modal to go back to the secondViewController(a subclass of HomeController) it returns me back to selectedIndex 0, so not the selectedIndex 1 childViewController it was presented from. I would like the modal to dismiss and return the user back to childViewController it was presented from (the secondViewController) and not return back to selectedIndex 0. Thanks in advance!</p>
  
  <p>// NavigationConroller as rootViewController</p>
  
  <pre><code>class MainNavigationController: UINavigationController {
  
      var segmentedController: UISegmentedControl!
  
      override func viewDidAppear(_ animated: Bool) {
          super.viewDidAppear(animated)
  
          let vc1 = TravelersFeedVC()
          let vc2 = ProfileVC()
  
          if isLoggedIn() {
              // assume user is logged in
              let homeController = HomeController()
              viewControllers = [homeController]
              homeController.firstViewController = vc1
              homeController.secondViewController = vc2
  
          } else {
              perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
          }
      }
  
      fileprivate func isLoggedIn() -&gt;  Bool {
          return UserDefaults.standard.isLoggedIn()
      }
  
      func showLoginController() {
          let loginController = LoginController()
          present(loginController, animated: true, completion: {
              // perhaps do something here later
          })
      }
  }
  </code></pre>
  
  <p>// HomeController as parentViewController</p>
  
  <pre><code>class HomeController: UIViewController, FBSDKLoginButtonDelegate {
  
      // child view controllers to put inside content view
      var firstViewController: TravelersFeedVC?
      var secondViewController: ProfileVC?
  
      private var activeViewController: UIViewController? {
          didSet {
              removeInactiveViewController(inactiveViewController: oldValue)
              updateActiveViewController()
          }
      }
  
      private func removeInactiveViewController(inactiveViewController: UIViewController?) {
          if let inActiveVC = inactiveViewController {
              // call before removing child view controller's view from hierarchy
              inActiveVC.willMove(toParentViewController: nil)
  
              inActiveVC.view.removeFromSuperview()
  
              // call after removing child view controller's view from hierarchy
              inActiveVC.removeFromParentViewController()
          }
      }
  
      private func updateActiveViewController() {
          if let activeVC = activeViewController {
              // call before adding child view controller's view as subview
              addChildViewController(activeVC)
  
              activeVC.view.frame = contentView.bounds
              contentView.addSubview(activeVC.view)
  
              // call before adding child view controller's view as subview
              activeVC.didMove(toParentViewController: self)
          }
      }
  
      // UI elements
      lazy var contentView: UIView = {
          let tv = UIView()
          tv.backgroundColor = UIColor.purple
          tv.translatesAutoresizingMaskIntoConstraints = false
          tv.layer.masksToBounds = true
          return tv
      }()
  
  
      var segmentedController: UISegmentedControl!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          activeViewController = firstViewController
  
          checkIfUserIsLoggedIn()
  
          view.addSubview(contentView)
  
          setupProfileScreen()
  
          let items = ["Travelers", "Me"]
          segmentedController = UISegmentedControl(items: items)
          navigationItem.titleView = segmentedController
  
          segmentedController.tintColor = UIColor.black
          segmentedController.selectedSegmentIndex = 0
  
          // Add function to handle Value Changed events
          segmentedController.addTarget(self, action: #selector(HomeController.segmentedValueChanged(_:)), for: .valueChanged)
  
          navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .plain, target: self, action: #selector(handleSignOut))
          navigationItem.leftBarButtonItem?.tintColor = UIColor.black
  
      }
  
  
      // reference to collectionViewController
      var travelersFeedVC: TravelersFeedVC!
  
      func segmentedValueChanged(_ sender:UISegmentedControl!)
      {
          switch segmentedController.selectedSegmentIndex {
          case 0:
              activeViewController = firstViewController
  
          case 1:
              activeViewController = secondViewController
  
          default: // Do nothing
              break
          }
      }
  </code></pre>
  
  <p>// secondViewcontroller in containerView where modal is presented from</p>
  
  <pre><code>class ProfileVC: UIViewController {
  
  // button to present modal
      lazy var placesButton: UIButton = {
          let customButton = UIButton(type: .system)
          customButton.backgroundColor = UIColor.clear
  //        customButton.frame = CGRect(x: 150, y: 50, width: 120, height: self.view.frame.height)
          customButton.setTitle("## of Places", for: .normal)
          customButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
          customButton.setTitleColor(.white, for: .normal)
          customButton.addTarget(self, action: #selector(handleShowPlacesVC), for: .touchUpInside)
  
          return customButton
      }()
  
  // function to call to present modal
      func handleShowPlacesVC() {
          let placesVC = PlacesTableVC()
          let navigationController = UINavigationController(rootViewController: placesVC)
          present(navigationController, animated: true, completion: nil)
      }
  </code></pre>
  
  <p>// modal view to dismiss</p>
  
  <pre><code>   override func viewDidLoad() {
          super.viewDidLoad()
  
          navigationItem.leftBarButtonItem = UIBarButtonItem(title: "back", style: .plain, target: self, action: #selector(handleCancel))
  
      }
  
  // dismiss modal view to return to secondViewController in childViewController containerView
      func handleCancel() {
          dismiss(animated: true, completion: nil)
      }
  </code></pre>