iPhone: How to Pass Data Between Several Viewcontrollers in a Tabbar App

25,953

Solution 1

You need a data model object that stores the data for application.

A data model is a customized, standalone object accessible from anywhere in the application. The data model object knows nothing about any views or view controllers. It just stores data and the logical relationships between that data.

When different parts of the app need to write or read data, they write and read to the data model. In your case, view1 would save its data to the data model when it unloads and then view2 would read that data from the data model when it loads (or vice versa.)

In a properly designed app, no two view controllers should have access to the internal data of another controller. (The only reason a view controllers needs to know of the existence of another controller is if it has to trigger the loading of that other controller.)

The quick and dirty way to create a data model is to add attributes to the app delegate and then call the app delegate from the view controllers using:

YourAppDelegateClass *appDelegate = [[UIApplication sharedApplication] delegate];
myLocalProperty = appDelegate.someDataModelProperty;

This will work for small project but as your data grows complex, you should create a dedicated class for your data model.

Edit:

To clarify for your specific case, you would add the call to the data model when the receiver viewController becomes active.

Placing the data in an init method or a viewDidLoad won't work because in a UITabBar the users can switch back and forth without unloading the view or reinitializing the view controller.

The best place to retrieve changing data is in the viewWillAppear controller method. That way the data will be updated every time the user switches to that tab.

Solution 2

You might want to consider NSNotificationCenter (Reference); you register the one viewcontroller with the application notification center, and send a notification when a selection is made. When the notification is received, the other viewcontroller updates itself accordingly.

Share:
25,953
Simon D.
Author by

Simon D.

Updated on July 09, 2022

Comments

  • Simon D.
    Simon D. almost 2 years

    I have following problem:

    I have built a tabbar application with 4 tabs. I want to pass a object/variable from the first tab controller to the third one and initialize this controller with the corresponding object.

    I've already done some research. The best way, corresponding to a clean model approach, would be to call some initWithObject: method on the called viewcontroller. How can I achieve this? How can I call the init method of the receivercontroller within the callercontroller? Can you give me some code example?

    Edit: To pass data between several views/classes etc simply create some Kind of data class which holds the data beeing shared between several classes. For more information follow the link: Singleton

  • Simon D.
    Simon D. about 14 years
    Thats true, but i think you cannot pass objects through NSNotificationCenter. So i could send some Notification to the receiverController. But then i still have the problem that i need my init parameters to initiate the new viewcontroller.
  • Williham Totland
    Williham Totland about 14 years
    You would be wrong. You can perfectly well pass objects through NSNotificationCenter. You can use, for example; - (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo, or just + (id)notificationWithName:(NSString *)aName object:(id)anObject
  • TechZen
    TechZen about 14 years
    This will work but it is not good practice because the complexity of the references between the view controllers will snowballl as you add views. Adding or removing one view controllers means updating several others. One view controller can mangle the data held in another.
  • acheo
    acheo about 14 years
    @TechZen yep sounds reasonable to me
  • Simon D.
    Simon D. about 14 years
    Thanks a lot. I use now the singleton approach. I've also posted some link to a good tutorial which describes, which steps have to be done to create a singleton data model.