Kousik
Kousik

Reputation: 22465

Container View with multiple child views issue

I am trying to develop a Table View Controller(having a navigation controller),where rows are connected to multiple View Controllers (TextField,TextView,TableView,DatePicker,ImageView etc.).

So I design like this,if I click on any row,it should open one UIViewController having container view and then place the appropriate controller in the container.All the same type of tableview rows are using same View Controller as a child view of the container.

I am able to place proper view controller(example - 1.TextViewController for Text View 2. Table View Controller for Table view 3. DatePickerController for Date Picker.) in the container depend on their the row type.

But I am little bit confuse about how to pick the data from the child view when I click the done button(2nd screen right top). i.e for child Text Field I have to pick the input data whatever I type in the input box. For child Table view I hide the done button,so as soon as user select the data 'cellForRowAtIndexPath' should get fire and pass the seleted data.

How to do that data handleing? where to write that? Is there any other way to design this?

enter image description here

Upvotes: 1

Views: 321

Answers (1)

danypata
danypata

Reputation: 10175

As @Suhail mentioned the best way to do it, in general, when you want to pass data from a child view controller to the parent view controller, or in some cases from a controller to previous displayed controllers (that are still in the stack), is by using delegate pattern. You can implement delegate pattern with iOS protocols or with blocks. In my opinion, both approaches have their pros and cons, for that topic you'll have to do little more google search since this is not the place to discuss it.

Let's define some cases for your case(not all the cases):

  1. You want to send data from ChildTableViewControler to Field controller (screen 3 to screen 2)

    In this case, from what I understood, both controllers are embedded in a parent controller, so you'll have to set parent to be the delegate to the two child controllers. You have to create one or two protocols depending on your actions or data you want to send to the controllers. Create a property called delegate (you can choose your own name) on each of the children, implement the methods on the parent view controller, whenever you add one of the children on screen, set the delegate property to be the parent view controller. Now whenever you want to send data to the other child, you'll have to call the methods declared in your protocols. Remember, you have access from the parent to both children via childViewControllers propery.

    Short version: One/Two protocols for children, parent implements the protocol(s) and responds to child action.

  2. You want to send data from Filed to TableViewController (from screen 2 to screen 1)

    In this case you'll declare a protocol in the parent view controller, which will be implemented by the TableViewController.Declare a delegate (or whatever name you like) property in the parent view controller. When you add the Filed controller on screen, you set the delegate property to be the TableViewController. Now you can communicate with the TableViewController from the Field controller via delegate property.

    Short version: one protocol in parent view controller, TableViewController implements the protocol and responds to TableViewController actions.

  3. You want to send data from ChildTableViewController to TableViewController (screen 3 to screen 1). This is the same as case 2.

One of my rule when I send data from view controllers is something like this: if I want to send data forward (to the next screen that will be displayed) then I use property/methods. If I want to send data backwards(to previously displayed controllers) then I use delegate/blocks.

And my last advice, please check the delegate/blocks implementations and how to use them before you start implementing one of the above solutions. You can have lots of troubles if you implement them wrong, especially memory issues and random crashes.

A little bit off topic, if the reader of my answer is a 9gagger then "sorry for the long post, here's a potato"

Upvotes: 2

Related Questions