从childView传递数据到parentView!

发布于 2024-09-16 07:03:35 字数 99 浏览 4 评论 0原文

当用户单击后退按钮时,我无法将数据从子视图传递到导航控制器内的父视图。

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞎闹 2024-09-23 07:03:35

有两种基本技术可以实现此目的:

使用委托

为您的孩子定义一个简单的委托协议。例如,如果您的孩子习惯于从事物列表中选择某个字符串,则执行以下操作:

@protocol ChildViewControllerDelegate <NSObject>
-(void) childView: (ChildView*) didPickNameFromList: (NSString*) name;
@end

您的父母将简单地实现此委托协议,并且在客户端中进行选择时将进行更新。

这是一种非常标准的技术,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:

@protocol ChildViewControllerDelegate <NSObject>
-(void) childView: (ChildView*) didPickNameFromList: (NSString*) name;
@end

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.

烟酉 2024-09-23 07:03:35

事实上我做了一些不同的事情。

我在子控制器中声明了一个具有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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文