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 Th开发者_开发技巧anks for you help. Mike.
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?
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"];
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];
}
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.
精彩评论