通过应用程序代理在视图之间共享数据 [英] Sharing Data between Views through App Delegate

查看:82
本文介绍了通过应用程序代理在视图之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在视图之间共享数据的问题,希望它不是太基本。我有一个在一个appDelegate中的NSMutableArray与说(Object1,Object2,Object3)。当一个新的对象被创建(并添加到数组),我需要访问该对象在下一个视图。以前,我只是将对象传递给下一个视图,但是有点混乱,似乎是错误的。

I have a question about sharing data between views, hopefully it's not too basic. I have an NSMutableArray in an appDelegate with say (Object1, Object2, Object3). When a new Object is created (and added to the array), I need to access that Object on the next view. Previously I was just passing the Object to the next view, but that got messy and seemed wrong.

现在我有所有的对象在一个地方,但如何访问在下一个视图中的对象(刚刚创建),因为我不知道它被添加到什么索引?每次创建对象时,我需要检查数组的数量,然后从中确定数组索引并将该索引值传递到下一个视图?或者有一种方法可以通过某种类型的唯一名称从NSMutable数组中检索对象?

So now I have all my objects in one place, but how can I access the Object (that was just created) in the next view because I don't know what index it was added into? Do I need to check the count of the array each time I create an object, then determine the array index from that and pass that index value to the next view? Or is there a way to retrieve objects from a NSMutable array by a unique name of some sort?

另一个解决方案是迭代所有对象,直到找到一个我想要的,但我想知道什么是正确的方法做到这一点。感谢您的时间。

Another solution would be to iterate over all of the Objects until I find the one that I want, but I'm wondering what is the right way to do this. Thank you for your time.

编辑:感谢您的回应,我将澄清我想做什么。我有一个NavigationController和每个视图,你进入需要访问对象。你可以像一本书一样思考它。您选择要阅读的图书的第一个视图,然后是您选择的图书的页面。在App Delegate中是所有书的数组。你会通过每个页面查看这本书吗?

Thanks for the response, I'll clarify what I'm trying to do. I have a NavigationController and each view that you go into needs access to the Object. You could think about it like a book. The first view you select the book you want to read, and then each view thereafter is a page from the book you selected. And in the App Delegate is the array of all the books. Would you pass each "Page" view the book?

请记住,这是一个粗略的例子,因为当你选择的书,你已经知道它的索引在。

Keep in mind this is kind of a rough example because when you select the book you would already know the index that it was at. Say if you create a book, then you don't know where in the array it is at yet.

推荐答案

如果你能创建一本书,那么你不知道它在数组中的位置。为每个对象创建一个有意义的唯一名称,只需使用 NSMutableDictionary ,并让控制器通过指定键来获取数据。像这样:

If you can create a meaningful unique name for each of those objects, just use a NSMutableDictionary and have the controllers get to the data by specifying the key. Like so:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString *obj = [appDelegate.dict objectForKey:@"unique-name-here"];

希望有帮助。

这篇关于通过应用程序代理在视图之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆