Communication between container controllers

I am new to iOS development and I have a problem. Here is my application. screenshot What I am trying to achieve is when I click on the radio button it should update the probabilities of the table view.

Here is my storyboard: storyboard

I can get the modified value of the radio button in DetailViewController (left controller, ancestor) using the delegate protocol. But now I have no idea how to pass the new value to DetailChartViewController. DetailChartViewController is a UIPageViewController containing UITableViewControllers (as shown in the screenshot). So how can I broadcast the new value from child to parent? Should I use the observer pattern? Action Center?

Any suggestion would be helpful.

+3


source to share


1 answer


Create a var for your detailChartViewController then set it directly. You can assign var in your prepareForSegue method. When you create a container view, you need to set its inline controller ID. Then the prepareForSegue method will be activated in which you assign this var to the controller. If you want to access your parent from children, you can either pass a reference to your parent from the prepareForSegue function or create a protocol / delegate for the feedback (this is usually preferred).



var detailFormViewController:DetailFormViewController? // Set the identifier for both of these in the identity inspector of these views in the storyboard
var detailViewController:DetailViewController?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     if segue.identifier == "DetailView" {
         self.detailViewController = segue.destinationViewController as! DetailViewController
         // You can always pass the parent to the child like below although a delegate is a more preferred technique
         self.detailViewController.parentViewController = self
     } else if segue.identifier == "DetailFormViewController" {
         self.detailFormViewController = segue.destinationViewController as! DetailFormViewController
     }
}

func someFunction() {
     // Now that you have a reference to your container view controllers you can access any of their objects directly from your parent view. 
     self.detailViewController.labelSomething.text = "Something"
     self.detailFormViewController.labelSomethingElse.text = "Something else". 
}

      

+4


source







All Articles