如何在视图控制器之间传递数据 [英] How to pass data between viewcontrollers
问题描述
这时我有一个相当复杂的应用程序,感觉好像我处理数据的方式还不够.
I have a fairly complex application at this point, and feel as though the way I am handling data is not up to scratch.
因此,我已开始采取一些措施使我的应用程序对MVC更友好.首先,我已经从所有ViewController中都具有内联解析器方法转变为创建专用的解析器类.
So I have started taking some steps towards making my app more MVC friendly. The first of which, I have gone from having inline parser methods in all of my ViewControllers to creating a dedicated parser class.
调用它并不难,我只需设置类,然后调用我创建的初始化方法,然后将其传递给我的请求即可..我的对象类中的所有内容都能正常工作,我从中获取了一大堆返回数据我正在解析的服务器..但这就是我迷路的地方.
Its not hard to call I just set up the class then call the initializer method I have made which I pass the data to for my request.. everything in my object class works sweet I get a whole bunch of return data from the server that I am parsing.. but then this is where I get abit lost.
如何将这些数据返回给调用它的ViewController?我已经与协议和委托进行了一些合作..但是我不知道这将如何适应这个问题.
How do I get that data back to the ViewController that called it? I have worked a little with protocols and delegates.. but I dont know how that would fit into this.
这是我要实现的目标的图形示例.
Here is an graphical example of what I am trying to achieve.
因此,如上所述,View控制器调用连接到DB的对象类的初始化方法下载数据,然后解析该数据.我遇到的问题是如何将数据发送到ViewController2.
So as above View controller calls the initalization method of the object class which connects to the DB downloads the data, and then parses that data. The issue I am having is how do I then get that data to ViewController2..
最合适的方法和将来的证明方法是什么?
what is the most appropriate and future proof way of doing this?
推荐答案
一个自定义- init
方法确实产生了奇迹.例如,我需要实例化并从上一个屏幕中推送一个预加载了数组的选择器,因此我在第二个视图中定义了-(id)initWithArray:(NSArray *)array
,然后将其称为 secondView = [[SecondView alloc] initWithArray:_population];
然后将其推送.记住要在释放和销毁传递的任何对象之前获得所有权!
A custom -init
method does wonders. For instance, I needed to instantiate and push a picker preloaded with the array from the previous screen, so I defined -(id)initWithArray:(NSArray*)array
in the second view, then just called secondView = [[SecondView alloc]initWithArray:_population];
then pushed it. Remember to get ownership of any object you pass before it is deallocated and destroyed!
这篇关于如何在视图控制器之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!