1

I have two views, one is a main view, and another is a container view. What I wish to do is send a variable to the child and for it to execute my function. How do I tell the child view these two things from the parent view?

A few problems I have had is, ViewDidLoad does not work in container view. and when I tried to segue the information it also said that a periotical programming or container has been implemented indicating to me that the container can not be segued.

What I am trying to do is have two tables, the first table has a list, and the second table changes depending what you select in the first table. So I decided to seperate the two tables using a container view which is working for the most part.

Shinerrs
  • 129
  • 2
  • 13
  • You have to clarify what you mean by "container view". Are you dragging out a container view in IB, and getting a view controller embedded in that? You also need to be careful in your language; viewDidLoad is a view controller method, so it wouldn't be called in "container view", unless you're really talking about a view controller; don't use "view" when you mean "view controller". – rdelmar Apr 10 '15 at 18:06

2 Answers2

1

First of all, you should note that any containers you create will be available through the childViewControllers property on your main controller. This gives you a link between the main controller and the child controller.

Now what you are trying to do is to change something in the child's table if the parent's table changes. There are many ways to do that, but in this case there is a pretty simple technique: you always know when the parent's table cell selection changes, in didSelectRowAtIndexPath. So what you need to do is simply within didSelectRowAtIndexPath, when the row changes you call a method in the child controller that passes in whatever information about the currently selected row you want to use. That method will change the table and do whatever else you want to do within the child view, since it's part of the child controller.

Using delegates or KVO or any other technique is overkill and bound to make the code much less readable.

Rikkles
  • 3,372
  • 1
  • 18
  • 24
0

You can use delegates to do that. Delegates are just protocols you can implement to pass data to another view:

protocol nameOfProtocol {
    func someFunc
}

You can search for a tutorial or examples easily, that way you can visualize it much better than just looking at some code here.

If you want viewDidLoad to work you can create a view in storyboard, give it an identifier name (and also setting it to the correct view controller) and you can declare it like:

let viewController = storyboard?.instantiateViewControllerWithIdentifier("viewControllerName") as! ViewControllerName
Eendje
  • 8,815
  • 1
  • 29
  • 31