从childView传递数据到parentView!
当用户单击后退按钮时,我无法将数据从子视图传递到导航控制器内的父视图。
我尝试使用 viewWillDisappear 但它没有被调用。
有什么想法吗?
I am having trouble passing data from a childView to the parentView inside a navigationController when the user click on the back button.
I tried using the viewWillDisappear and it is not being called.
Any ideias?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两种基本技术可以实现此目的:
使用委托
为您的孩子定义一个简单的委托协议。例如,如果您的孩子习惯于从事物列表中选择某个字符串,则执行以下操作:
您的父母将简单地实现此委托协议,并且在客户端中进行选择时将进行更新。
这是一种非常标准的技术,iOS 附带的许多视图控制器也使用它。例如,看一下
ABPeoplePickerNavigationControllerDelegate
。使用共享(容器)对象)
您可以使用的另一种技术是使用子视图控制器可以更改其中值的共享对象。父视图控制器创建了该对象并保留对其的引用。当它显示子视图控制器时,它将对共享对象的引用传递给它,以便子视图控制器可以在其上设置值。然后,当父级再次出现时,它可以根据该共享对象的值更新其状态。
我个人更喜欢技巧#1。
There are two basic techniques for doing this:
Using a Delegate
Define a simple delegate protocol for your child. Like for example if your child is used to pick some string from a list of things then do something like:
Your parent would simply implement this delegate protocol and will have a change to be updated when a selection is made in the client.
This is a very standard technique that is also used by many of the view controllers that come with iOS. For example take a look at the
ABPeoplePickerNavigationControllerDelegate
.Using a Shared (Container) object)
The other technique that you can use is to use a shared object in which the child view controller can change a value. The parent view controller created this object and keeps a reference to it. When it displays the child view controller then it passes a reference to the shared object to it so that the child can set values on it. Then when the parent appears again, it can update its status based on the values of that shared object.
Personally I prefer technique #1.
事实上我做了一些不同的事情。
我在子控制器中声明了一个具有parentController类型的变量。
在parentController 中,在推送childController 之前,我会执行以下操作:
objView.parentController = self;
然后我只需调用parentController.variableIwant = Something;关于子控制器和巴里纱!
Actually I did something different.
I declared a variable with the type of the parentController in the child controller.
And in the parentController before I push the childController I do this:
objView.parentController = self;
Then I just call the parentController.variableIwant = something; On the child controller and Voile!