iOS: Multiple views accessing the same data - how to access?
I'm a Java Developer new to iOS and objective c development.
I need to build an iOS application where multiple views (e.g. a ListView and a MapView) access the same data (e.g. stored in an NSArray).
Currently, I do alloc and init a DataManager class that loads the data (loading it from a plist, later it should be requested from the web) in the app delegate.
How do I access this data from the views? Should the DataManager be a singleton?开发者_开发知识库 Is there a better/more elegant/more "obj-c 2.0" solution?
Thanks!
Use the MVC architecture. Your data is a model object, which can be passed to the various controllers that use the data to populate the views. In Cocoa Touch, the views are the UIView
subclasses that you typically create in XIB files. The controllers usually start with UIViewController
instances that manage the views, but include things like table view delegates and data sources. The models are the objects you create to represent the data - your DataManager
class.
There are plenty of frameworks in the Java world that use the MVC pattern, so you may already be used to dividing classes up this way if you've come across e.g. Spring MVC, or Eclipse RCP.
精彩评论