One copy for several species in Cocoa Touch

I was wondering how I can access a single instance of a class, say "ship class", across multiple views.

Let's say I have a rootViewController and three subheadings, one Main, one battle, and one landing. Where exactly should I implement an instance of a class that stores all the information on my ship and access to it, although different views. If my ship has x missiles left as another kind of access to this information to display it. If I implement it on the main view, how does the landing view get this information?

I know there should be an easy way to do this, and I'm sure you should do it without applying the ship class in any of the views. I'm still pretty new to iPhone programming.

Thank you in advance for any help you can provide.

+1


a source to share


2 answers


After you've created a single instance of the Ship class, you need to tell the other views what the instance is. This way you can make Ship a public property of the class that created it.

@property (nonatomic, retain) Ship* myShip;

      

Suppose the class is an application delegate - then in another file you should write something like

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

      



It's simple and works for a simple project, but makes the Ship object available to any file in your project - effectively becoming global data that is difficult to debug on a larger project. (If something bad happens to myShip, which object was responsible? If it could be any object in your application, then it's a harder problem to debug than if access is limited to the two classes that need it.) So, if you want to restrict access to the ship, then don't make myShip public - instead, the object that created myShip passed it on on initialization when it creates other objects that need access. Something like that

[[myBattleController alloc] initWithShip:myShip];

      

If it really is one and only one ship object for the entire application, search Stack Overflow for advice on creating singleton objects.

+3


a source


Your ship class is a Model in MVC (Model View Controller). This will be a separate class. Your 3 views are part of the MVC view. All you are missing is a controller. The controller creates your model (like a ship class) and your views, and contains links to all of that.

The controller is the "hub" of your application. It receives user input from the views and performs operations on the model, and then passes the views to update the display.



There are several approaches to transferring data for viewing. There are two possible options: add view methods to pass data (for example, show some information in a view). The second option, probably more applicable here, is to reference the model for each point of view - this is used for read-only access only. For instance. when you need to revise the view (s) they get accurate data from the model (ship class).

Look at the MVC pattern, there is a lot of information about it even in the Apple documentation.

+1


a source







All Articles