简体   繁体   English

具有多个子视图的容器视图问题

[英]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.). 我正在尝试开发一个表视图控制器(具有导航控制器),其中行连接到多个视图控制器(TextField,TextView,TableView,DatePicker,ImageView等)。

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. 所以我这样设计,如果我单击任何行,都应该打开一个UIViewController having container view然后将适当的控制器放入容器中。所有相同类型的tableview行都使用相同的View Controller作为容器的子视图。

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. 我能够在容器中放置适当的view controller (例如-用于文本视图的1.TextViewController 2.用于表视图的Table View控制器3.用于日期选择器的DatePickerController。)取决于它们的行类型。

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). 但是,当我单击done button (右上角的第二个屏幕)时,我对如何从子视图中选择数据有点困惑。 ie 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. 对于子表视图,我隐藏了完成按钮,因此,一旦用户选择数据“ cellForRowAtIndexPath”,则应立即触发并传递所选择的数据。

How to do that data handleing? 该如何处理数据? where to write that? 在哪里写? Is there any other way to design this? 还有其他设计方法吗?

在此处输入图片说明

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. 正如@Suhail提到的最佳方法,通常,当您要将数据从子视图控制器传递到父视图控制器时,或者在某些情况下从控制器传递到以前显示的控制器(仍在堆栈中) ,是使用delegate模式。 You can implement delegate pattern with iOS protocols or with blocks. 您可以使用iOS协议或块来实现委托模式。 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. 我认为,这两种方法都有其优点和缺点,对于该主题,您将不必做更多的Google搜索,因为这不是讨论它的地方。

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) 您想要将数据从ChildTableViewControler发送到Field控制器(屏幕3到屏幕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. 在这种情况下,据我了解,两个控制器都嵌入在父控制器中,因此您必须将parent设置为两个子控制器的委托。 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. 在每个子级上创建一个称为“ delegate的属性(您可以选择自己的名称),在父级视图控制器上实现方法,每当您在屏幕上添加一个子级时,请将“ delegate属性设置为父级视图控制器。 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. 请记住,您可以通过childViewControllers从父级访问两个孩子。

    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) 您想要将数据从Filed发送到TableViewController (从屏幕2到屏幕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. 在这种情况下,您将在父视图控制器中声明一个协议,该协议将由TableViewController实现。在父视图控制器TableViewController声明一个delegate (或您喜欢的名称)属性。 When you add the Filed controller on screen, you set the delegate property to be the TableViewController . 在屏幕上添加Filed控制器时,将delegate属性设置为TableViewController Now you can communicate with the TableViewController from the Field controller via delegate property. 现在,您可以通过delegate属性从Field控制器与TableViewController通信。

    Short version: one protocol in parent view controller, TableViewController implements the protocol and responds to TableViewController actions. 简短版本:父视图控制器中的一种协议, TableViewController实现该协议并响应TableViewController动作。

  3. You want to send data from ChildTableViewController to TableViewController (screen 3 to screen 1). 您想要将数据从ChildTableViewController发送到TableViewController (屏幕3到屏幕1)。 This is the same as case 2. 这与情况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. 最后我的建议是,在开始实现上述解决方案之一之前,请检查delegate / blocks实现以及如何使用它们。 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" 如果您的答案是9gagger,那么“话题很长,很抱歉,这是个土豆”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM