Q:

From a containerView, how do you access the view controller containing the container in Swift?

I do have 4 Views with a Headerpart which I outsourced into a containerview to have the same fields and layout on all 4 views. Inside my container im having a lot of labels which i know wanna fill with data. My problem now is, that i have to fill the labels accordingly to game the user selected. game is a enum inside my player class. I have no idea how i can gain that information from inside my containerview and set the game variable accordingly to perform my code. Is there a solution to get the storyboardid from the view my containerview is on out of the containerview?

switch game
case .Coinflip:
Player1PointsLabel.Text = (player1.points.coinflip)
case .RollingDices
Player1PointsLabel.Text = (player1.points.rollingdices) 

Maybe i did something wrong, design wise, i'm not that experienced yet, so i'm also open for advises.
Best regards

A:

As far as I know, the only way to get the ViewController of a view that was inserted into a ContainerView, is to save a reference to it in the parent ViewController when the ContainerView is instantiated.
Swift 4 Examples:
If you used a ContainerView in a storyboard and added an embed segue:
var containerVC: UIViewController?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "YourEmbedSegueName" {
        if let vc = segue.destination as? YourViewController {
            self.containerVC = vc
        }
    }
}

Or, if you inserted a View inside a ContainerView programmatically:
var containerVC: UIViewController?
func customContainerEmbed(vc: UIViewController) {
    self.addChildViewController(vc)
    yourContainer.addSubview(vc.view)
    vc.view.frame = yourContainer.bounds
    vc.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    vc.didMove(toParentViewController: self)

    self.containerVC = vc
}

A:

The goal of your question is not very clear.
If you want to access the superview of your view (the view containig your subview), then use 'myView.superview'.
If you want to access the UIViewController that host your UIViewController, then use 'myViewController.presentingViewController'.
Finally, if you want to access the UIViewController hosting your view, you must walk the responder chain until you reach the first UIViewController or the end of the chain (UIView is a subclass of UIResponder):
func viewController(forView: UIView) -> UIViewController? {
  var nr = forView.next
  while nr != nil && !(nr! is UIViewController) {
    nr = nr!.next
  }
  return nr as? UIViewController
}

