views:

191

answers:

2

I was wondering how I would go about accessing one instance of a class, say "ship class" across multiple views.

Lets say I have a rootViewController and three subviews, one Main, one battle, and one landing. Where exactly would I implement an instance of a class that stores all the information on my ship and access it though the other views. If my ship has x missiles left, how does another view access that information to display it. If I implement it on the Main view how does the landing view get that info?

I know there has to be an easy way to do this, and I bet there is a way you are supposed to do it without implementing the ship class in any of the views per se. I am still fairly new to programming for the iPhone though.

Thank you in advance for any help you all may provide.

+1  A: 

Your ship class is the Model in MVC (model view controller). It would be a standalone class. Your 3 views are the view part of MVC. All you're missing is a controller. Controller creates your model (e.g. ship class) and your views and holds references to all this.

Controller is the 'hub' of your application. It receives user input from views and executes operations on model then tells views to refresh display.

There are couple of approaches to pass the data to view. Two possible options: add methods to view to pass data to it (e.g. show some info on the view). Second options, probably more applicable here, is for each view to keep reference to model - this is only used for 'read-only' access. E.g. when your view(s) need to be redisplayed they get the exact data from the model (ship class).

Have a look at MVC pattern, there's lot of info on it even in apple documentation.

stefanB
+3  A: 

Once you have created the single instance of your Ship class, you need to tell any other views what that instance is. So you could make Ship a public property on the class that created it.

@property (nonatomic, retain) Ship* myShip;

Suppose that class is the app delegate - then, in another file, you'd write something like

[[[UIApplication sharedApplication] delegate].myShip getNumberOfMasts];

This is simple, and works for a simple project, but it does make the Ship object available to any file in your project - it effectively becomes global data, which can be hard to debug in a larger project. (If something bad is happening to myShip, which object was responsible? If it could be any object in your app, then this is a harder problem to debug than if access is restricted to the two classes that need it.) So, if you want to restrict access to ship, then don't make myShip a public property - instead have the object that created myShip pass it in initialisation calls when it creates other objects that need access. Something like this

[[myBattleController alloc] initWithShip:myShip];

If this really is one and only one ship object for the whole application, search on stack overflow for advice on creating singleton objects.

Jane Sales
If it's more than one ship a singleton object with an array of ships would be the way to go as well.
tmadsen