简体   繁体   English

tableView和普通视图(相同的数据对象)

[英]tableView and normal view (same data object)

I don't know how I can share the same data between two tab bar items. 我不知道如何在两个标签栏项目之间共享相同的数据。 One tab uses nsfetchedresultscontroller and the other table is just a normal view that should use the same data as my fetchedresultscontroller. 一个选项卡使用nsfetchedresultscontroller,另一个表只是普通视图,应该使用与我的fetchedresultscontroller相同的数据。

If i had two normals views, I would just make my object a singleton, and let the tableview and normal view use the same data. 如果我有两个法线视图,则只需将我的对象设为单例,然后让tableview和normal视图使用相同的数据。

But now i have a fetchresultscontroller and a normal view. 但是现在我有一个fetchresultscontroller和一个普通视图。 I was thinking about just using another fetchedresultscontroller for my normal view, so I have all the features such as fetch cache, notifications and such.. 我当时在考虑只将另一个fetchedresultscontroller用于正常视图,因此我具有所有功能,例如访存缓存,通知等。

I know fetchresultscontroller should be used for tableview, but I guess I can use it for non tableviews as well. 我知道fetchresultscontroller应该用于表视图,但我想我也可以将其用于非表视图。

Somebody knows what it is I should do? 有人知道我该怎么办? I couldn't find documentation from apple on this matter. 在这件事上,我找不到苹果的文件。

Your data should have noting to do with your viewControllers or views - that is, if you follow the MVC style. 您的数据应该与您的viewControllers或视图有关-也就是说,如果您遵循MVC样式。 If your data is only available in one viewController and you don't want to extract it out into its own class, then provide methods in your viewController that can be called by the other viewController to get the data. 如果您的数据仅在一个viewController中可用,并且您不想将其提取到其自己的类中,请在viewController中提供可以被另一个viewController调用以获取数据的方法。

For example, have methods like this: 例如,使用如下方法:

- (NSString) getNameOfSelectedUserAtIndex:(int) index;
- (int) getCountOfSelectedUsers;
- (NSSArray) getResults;

And then from within your other viewController, call: 然后在另一个viewController中,调用:

string = [fetchedresultscontroller getNameOfSelectedUserAtIndex:i];
count = [fetchedresultscontroller getCountOfSelectedUsers];
array = [fetchedresultscontroller getResults];

You have to make sure you add fetchedresultscontroller as an instance variable to your other viewController and set it probably in its viewDidLoad. 您必须确保将fetchedresultscontroller作为实例变量添加到另一个viewController中,并可能在其viewDidLoad中进行设置。

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

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