Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 829
XaNNy0 Avatar asked Oct 12 '17 09:10

XaNNy0


3 Answers

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
}
like image 168
Erick Maynard Avatar answered Oct 23 '22 00:10

Erick Maynard


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
}
like image 4
Nicolas Buquet Avatar answered Oct 23 '22 01:10

Nicolas Buquet


Implement the prepareForSegue method of your main controller.

Based on the segue name, you can create a reference to the destination controller, managing you container view

like image 1
CZ54 Avatar answered Oct 23 '22 01:10

CZ54