3

I am new to iOS development and I am stuck with a problem. Here is my application. screenshot What I am trying to achieve is when I click on the switch, it should update the tableview probabilities.

Here is my storyboard : storyboard

I am able to get the changed value of the switch in the DetailViewController (the controller on the left, the ancestor) with a delegate protocol. But now I have no idea on how to broadcast the new value to the DetailChartViewController. The DetailChartViewController is a UIPageViewController containing UITableViewControllers (as seen in the screenshot). So how can I broadcast a new value from child to parent to child ? Do I have to use an observer pattern ? The notification center ?

Any suggestion will be helpful.

itsji10dra
  • 4,603
  • 3
  • 39
  • 59
prbaron
  • 867
  • 8
  • 19
  • Have a look at this for how to get a reference to your contained view controller: http://stackoverflow.com/questions/29582200/how-do-i-get-the-views-inside-a-container-in-swift/29582305#29582305 – ABakerSmith Apr 16 '15 at 16:31
  • This is a poorly worded question. It's hard distinguishing if you are talking about the parent and child. – Yash Jain Jul 30 '20 at 23:44

1 Answers1

4

Create a var for your detailChartViewController then set it directly. You can assign the var in your prepareForSegue method. When you create a container view you need to set it's identifier for the embedded controller. Then your prepareForSegue method will fire which is where you assign that var to the controller. If you want to access your parent from the children you can either pass a reference to your parent from within the prepareForSegue func or create a protocol/delegate to communicate back(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". 
}
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
  • Well I did this, but I do not understand how it can work. Because my DetailViewFormController propagates event to its parent, but how to broadcast and update view of the other childs ? If I am right, the prepareForSegue is called on parent view creation, so called only once. – prbaron Apr 16 '15 at 17:46
  • I'll update my answer with a more detailed explanation. – Mark McCorkle Apr 16 '15 at 18:03
  • Wouldn't it make sense to use delegates instead of setting direct references? Also Should we consider using weak references since they'll have circular dependencies or will it be handled? – Warpzit Jun 10 '16 at 06:11