简体   繁体   English

在堆叠视图之间传递数据

[英]Passing data between stacked views

In my app, I've several views.. and some of these views are pushed and popped using the navigation controller. 在我的应用中,我有几个视图..其中一些视图是使用导航控制器推送和弹出的。 In some view, I have a table view, in which each one of its cells is a view itself, so when the cell is selected, this code is executed : 在某些视图中,我有一个表视图,其中每个单元格都是一个视图本身,因此当选择该单元格时,将执行以下代码:

DetailView *detailViewController = [[Detailview alloc] initWithNibName:@"Detailview" bundle:nil];
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];

and in the detailview this code will be executed, when a specific button is clicked (eg back/cancel button): 在详细视图中,当单击特定按钮(例如,后退/取消按钮)时,将执行此代码:

[self.navigationController popViewControllerAnimated:YES];

What I want is passing data from the detailview to the view that precedes it(the view that initialize it). 我想要的是将数据从detailview传递到它之前的视图(初始化它的视图)。 So far, I've implemented a class named "Globals.h", in which I put the data I want to pass, and uses "extern" on these data variables to ensure it'll be global to many classes, and it worked properly. 到目前为止,我已经实现了一个名为“ Globals.h”的类,在其中放置了我想传递的数据,并对这些数据变量使用“ extern”以确保它对于许多类都是全局的,并且可以正常工作正确地。 But I don't feel that this is the appropriate way to do so. 但是我不认为这是合适的方法。 Is there any other good ways to do that ? 还有其他好的方法吗?

Thanks in advance :) 提前致谢 :)

One easy way to solve it is: 一种简单的解决方法是:

DetailView *detailViewController = [[Detailview alloc] initWithNibName:@"Detailview" bundle:nil];
detailViewController.controller = self;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];

and then in the detail view: 然后在详细视图中:

self.controller.data = data;
[self.navigationController popViewControllerAnimated:YES];

I hope you know how to create properties, otherwise drop me a comment. 希望您知道如何创建属性,否则请给我评论。

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

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