简体   繁体   English

将变量从模式表视图传递到父视图

[英]Passing a variable from modal tableview to parent view

I have a detailView which has a button on it to bring up a tableView as a modal view to help the user giving a selection of names. 我有一个detailView,上面有一个按钮,可以显示tableView作为模式视图,以帮助用户选择名称。 I'm trying to figure out a simple way of passing the selected name to a textField on the parent detailView. 我试图找出一种简单的方法,将所选名称传递给父detailView上的textField。

I have been building this project using Storyboard, but I am totally stuck at this point. 我一直在使用Storyboard构建该项目,但目前我完全陷入困境。

There are several ways to approach this kind of problem: 有几种方法可以解决此类问题:

  • shared object: The parent view controller provides some object to the child that contains the state that the child is expected to work with and possibly modify. 共享对象:父视图控制器为子级提供一些对象,该对象包含该子级可以使用并可能修改的状态。 In this case, it might be a dictionary that contains an array of names to display and the index of the selected person. 在这种情况下,它可能是一个字典,其中包含要显示的名称数组和所选人员的索引。

  • child's properties: Often, the child view controller itself is the shared object. child的属性:通常,子视图控制器本身就是共享对象。 The parent instantiates the child, sets some of the child's properties (eg people and selectedPerson ), and presents it. 父级实例化子级,设置子级的某些属性(例如peopleselectedPerson ),然后显示它。 When the child is finished, the parent gets the properties of the child that it cares about (eg selectedPerson ) before releasing it. 子级完成后,父级会在释放子级之前获取其关心的子级属性(例如selectedPerson )。

  • delegation: Sometimes the child view controller will need to interact with the parent in order to do its job. 委托:有时,子视图控制器将需要与父视图交互才能完成其工作。 This is a good time to use delegation: establish some protocol that the child knows about and the parent implements, add a delegate property to the child, and have the parent set the child's delegate property to itself before presenting it. 这是使用委派的好时机:建立孩子知道并由父母实现的某种协议,为孩子添加一个delegate属性,并让父母在展示之前将孩子的delegate属性设置为其自身。 This way, the child can talk to the parent without depending on the parent class. 这样,孩子就可以与父母交谈,而不必依赖于父母班级。

For the case you describe, where you just want to convey some piece of data back to the parent when the child is done, the second strategy above is the simplest. 对于您描述的情况,当您只想在孩子完成后将一些数据传送回父级时,上面的第二种策略是最简单的。

Note: DetailViewController below should be replaced with whatever the class is of your detail view controller. 注意:下面的DetailViewController应该用您的局部视图控制器的任何类替换。

1) If you don't already have a subclass of UITableViewController , create one. 1)如果还没有UITableViewController的子类,请创建一个。 This will allow you to add a property to the table view controller. 这将允许您将属性添加到表视图控制器。 Change the class of your modally presented controller in your Storyboard to this new subclass. 将情节提要中模态呈现的控制器的类更改为此新的子类。

2) Add a property to your table view controller subclass like this: 2)将属性添加到表视图控制器子类中,如下所示:

@property (weak, nonatomic) id mydelegate;

3) In prepareForSegue in your DetailViewController, set the mydelegate property of the destination view controller to self which is a pointer to your parent detailView. 3)在DetailViewController的prepareForSegue中,将目标视图控制器的mydelegate属性设置为self ,这是指向父detailView的指针。

4) In your DetailViewController, create a method to receive the selected name: 4)在您的DetailViewController中,创建一个方法来接收所选名称:

- (void)userDidSelectName:(NSString *)name;

5) In the tableView:didSelectRowAtIndexPath method of your tableViewController subclass, get the name from the tableViewCell and call the userDidSelectName method on the delegate: 5)在tableViewController子类的tableView:didSelectRowAtIndexPath方法中,从tableViewCell获取名称,并在委托上调用userDidSelectName方法:

[self.mydelegate userDidSelectName:selectedName];

6) The correct way to to this would be to define a protocol that has a method userDidSelectName and then make the delegate pointer in step 2 require a class that implements that protocol. 6)正确的方法是定义一个具有方法userDidSelectName的协议,然后使步骤2中的委托指针需要一个实现该协议的类。 The quicker and dirtier thing to do is to make the delegate pointer in step 2 of type DetailViewController * instead of id . 更快,更脏的方法是在步骤2中使委托指针的类型为DetailViewController *而不是id This will allow you to call userDidSelectName without the compiler complaining. 这将使您可以调用userDidSelectName而无需编译器抱怨。

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

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