views:

136

answers:

1

Hi guys,

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.

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.

But now i have a fetchresultscontroller and a normal view. 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..

I know fetchresultscontroller should be used for tableview, but i guess i can use it for non tableviews as wel.

Somebody knows what is should do? i couldn't find documentation from apple on this matter

A: 

Your data should have noting to do with your viewControllers or views - that is, if you follow the MVC style. 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.

For example, have methods like this:

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

And then from within your other viewController, call:

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.

mahboudz
Thanks for your anwser!, but i have to make a property in my appDelegate with a pointer to this controller to reach the nsfetchedresultscontroller, because the two controllers live in their own tabbar. I like your solution to get to the data, but how would i handle notifictions. If my fetchedresultscontroller receives update notifications my normal view should also receive this data, any thoughts on that?
I'm not sure I understand what you are doing. One way to do it would be to have your data as properties in your appDelegate. Then all you controllers would call, as above, but instead of fetchedresultscontroller, you cal you appDelegate. As for notifications, I have a main view controller which asks for notification and handles saving data when the app quits. I sometimes have that notification in my appDelegate or in my data model class. It all depends on what I am trying to do. In other words, I don't think you are forced to use a certain viewcontroller for your notifications.
mahboudz