iosswiftuicontainerviewparentviewcontroller

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


Solution

  • 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
    }