简体   繁体   English

通过 App Delegate 在视图之间共享数据

[英]Sharing Data between Views through App Delegate

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).我在 appDelegate 中有一个 NSMutableArray,比如(Object1,Object2,Object3)。 When a new Object is created (and added to the array), I need to access that Object on the next view.当一个新的 Object 创建(并添加到数组中)时,我需要在下一个视图中访问该 Object。 Previously I was just passing the Object to the next view, but that got messy and seemed wrong.以前我只是将 Object 传递到下一个视图,但这变得一团糟,似乎是错误的。

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?所以现在我将所有对象放在一个地方,但是我如何在下一个视图中访问 Object(刚刚创建),因为我不知道它被添加到哪个索引中? 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?每次创建 object 时是否需要检查数组的计数,然后从中确定数组索引并将该索引值传递给下一个视图? Or is there a way to retrieve objects from a NSMutable array by a unique name of some sort?或者有没有办法通过某种唯一名称从 NSMutable 数组中检索对象?

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.感谢您的时间。

Edit: 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.我有一个 NavigationController,您 go 进入的每个视图都需要访问 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.第一次查看您 select 您要阅读的书,然后之后的每个视图都是您选择的书的一页。 And in the App Delegate is the array of all the books.在 App Delegate 中是所有书籍的数组。 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.请记住,这是一个粗略的示例,因为当您阅读 select 这本书时,您已经知道它所在的索引。 Say if you create a book, then you don't know where in the array it is at yet.假设您创建了一本书,那么您还不知道它在数组中的哪个位置。

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.如果您可以为每个对象创建一个有意义的唯一名称,只需使用NSMutableDictionary并让控制器通过指定键来获取数据。 Like so:像这样:

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

Hope that helps.希望有帮助。

I'm not exactly sure if you're talking about sharing objects among different controls, or if you're talking about different view controllers - like a master-detail view, although I suspect this is what you mean.我不确定您是在谈论在不同控件之间共享对象,还是在谈论不同的视图控制器-例如主从视图,尽管我怀疑这就是您的意思。

In this case, if you have a detail view dedicated to editing a single object, it's not, in my opinion, wrong to give that view controller an object to edit.在这种情况下,如果您有一个专门用于编辑单个 object 的详细视图,那么在我看来,给该视图 controller 和 object 进行编辑并不是错误的。 If you're maintaining an array, you could still add the new object to the array, and assign the object to the new view controller before your put it on screen.如果您要维护一个数组,您仍然可以将新的 object 添加到数组中,并将 object 分配给新视图 controller,然后再将其放在屏幕上。

If you could be a little more specific about how your application works, I might be able to give you some better help.如果您可以更具体地了解您的应用程序是如何工作的,我可能会给您一些更好的帮助。

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

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