1

I have a tab bar application with 4 different tabs.(4 different views)

When my app first launches, the first thing I need to do is bring in my data which is located in a plist. This isn't a problem. My problem is, the data is displayed in each tab in a different way.

Where do you suggest the best place to load the data is?

Currently I'm using viewDidLoad of the first viewController. But from here, what do you think the best way to make the data available to the other views is? Perhaps make the other 3 views become a delegate for the 1st view?

Any suggestions would be much appreciated Thanks for you help. Mike.

4 Answers4

2

I don't know why each of the four views can't have a reference to the dictionary, and your application delegate loads from the plist and set the references. Is your data very large?

MHC
  • 6,297
  • 2
  • 23
  • 26
  • I had no idea you could this kind of setup in the appDelegate. This makes a lot of sense. I'm still in the learning stages of programming. Thank You. – Michael John Breault Feb 07 '11 at 17:43
2

The little data I've had that is truly global in this way I've made a property on my AppDelegate class. Your view controllers can all access it with

MyAppDelegate* delegate = [UIApplication sharedApplication].delegate;
id thing = [delegate.myDictionary objectForKey:@"someKey"];
Nick Curran
  • 456
  • 3
  • 11
  • The application delegate is meant for handling application lifecycle events and the like. It’s not a good design to use it to provide access to global data. In this case the data is not even global, it’s just shared between several controllers. See also my comment under [the singleton answer](http://stackoverflow.com/questions/4924350/whats-the-best-place-to-load-a-nsdictionary-you-want-to-be-accessible-to-all-vie/4924435#4924435). – zoul Feb 07 '11 at 17:40
  • This makes complete sense to me, thank you for your response. – Michael John Breault Feb 07 '11 at 17:44
1

This is an often asked question here. You should look at the MVC design pattern. Your dictionary would be a model in this scenario and all the controllers/views that need to access it should have their own property for it. The loading can be done in another controller/view with a progress bar, in the application delegate or in the first tab; that depends on your situation.

The classes would look a bit like this:

@interface Model : NSObject {…}
- (void) load;
@end

@interface ControllerA : UIViewController {…}
@property(retain) Model *model;
@end

@interface ControllerB : UIViewController {…}
@property(retain) Model *model;
@end

@implementation ApplicationDelegate

- (void) applicationDidFinishLaunchingAndWhateverElseIsUsuallyHere
{
    Model *model = [[Model alloc] init];
    [model load];

    ControllerA *controllerA = [[ControllerA alloc] init…];
    [controllerA setModel:model];
    ControllerB *controllerB = [[ControllerB alloc] init…];
    [controllerB setModel:model];
    [model release];

    // The syntax here is probably off, you should get the idea
    UITabBarController *tabs = …;
    [tabs setViewControllers:controllerA, controllerB, nil];

    [window addSubview:tabs.view];
    [window makeKeyAndVisible];
}
Community
  • 1
  • 1
zoul
  • 96,282
  • 41
  • 242
  • 342
  • I think I understand. I would create a model to hold my data. I could then use application delegate (or 1st tab view controller) to load my data into the model. Could you show me a piece of code that would allow me to access my model from a different view. Eg) I click on 2nd tab. How could I get a pointer to my model? – Michael John Breault Feb 07 '11 at 17:48
0

I don't have any experience with Tab Bar views, but it looks like you want to create a singleton class, so that you can access your dictionary globally.

Ned
  • 6,192
  • 2
  • 27
  • 34
  • I would advise against using enforced singletons to provide regular access to model objects, see the [singleton thread here on SO](http://stackoverflow.com/questions/137975) or [my blog post](http://zmotula.tumblr.com/post/1390385240). – zoul Feb 07 '11 at 17:34