I'm creating an iPhone game where the character moves between different views, so each view is essentially a new game in itself. But now I want to create health, so if health goes down in one view to say 5 hp, then when they change to a different view the health will still be 5. I've done some research and it sounds like what I need is a singleton or a global variable. But everything I've seen makes no sense to me. I have nothing in either of my app delegates, and I'm not using any frameworks like cocos2d or OpenGLES. Posting the code doesn't seem to be working right now, so if you could tell me what code you need to see to help you answer my question.
2 Answers
I've done some research and it sounds like what I need is a singleton or a global variable.
What you need is to share your character data between view controllers. A singleton is one way to share data; it's conceptually pretty simple but often comes with unfortunate consequences. See What is so bad about singletons? for a discussion of some of those consequences. See Alternatives to Singletons for some other approaches.
If you do decide to use a singleton (not recommended), you should be careful about choosing a role for that object. For example, your first thought might be to create a singleton that represents the player or character. However, that would mean that you could only ever have a single player or character in your game. A better choice would be to create a player manager, which you could use to access the current player.
A still better choice would be to forgo the singleton and give each view controller player
and gameController
properties (assuming that you have some object that's in charge of the overall game. The game controller would start each new "board" by instantiating the corresponding view controller, setting the gameController
property to itself, and setting the player
property to the current player object. That way, the view controller doesn't have to look outside itself to find out about the player. When its part of the game is finished, it can use its gameController
property to send a message to the game controller that says: "I'm done, you can move on to the next board" or whatever is appropriate.
-
My mind is blown by that answer.Do you think I could email you the project so you could see the code? Because i don't totally understand what you're sayin – Will Youmans Dec 20 '11 at 00:22
-
@WillYoumans Thanks for the vote of confidence. ;-) I'll pass on the e-mail, but once you understand the idea I'm sure you'll be able to implement it. You might look [here](http://stackoverflow.com/q/8421138/643383) and [here](http://stackoverflow.com/q/5210535/643383) for more, as well as reading up on MVC. You're certainly not the first person to wonder how to share data across different parts of their app -- you'll find a LOT of questions here on SO on this topic. – Caleb Dec 20 '11 at 00:56
Technically, anything can be access from anywhere in an app because we can access the app delegate like so:
#import "AppDelegate.h"
#define kAppDelegate (AppDelegate*)[[UIApplication sharedApplication] delegate]
so if you define a property in the app delegate like so:
@property (assign) CGFloat health;
you can access it like so...
CGFloat health = [kAppDelegate health];

- 881
- 1
- 10
- 21
-
So if I do this can I go into the viewDidLoad method in a separate view controller and say " health = 10;" – Will Youmans Dec 19 '11 at 23:39
-
2[kAppDelegate setHealth:xx.xx]; i think you should take a day to learn how objects work before proceeding – ssj Dec 19 '11 at 23:43
-
-
This is very bad advice. Using this solution, you will make every class you write dependent on your appdelegate. This will cause you a lot of pain if you decide to port to another platform (say, tvOS, OS X). This is an example that keeps popping up here on SO, but it is a sign of very bad structure and should be avoided. See for example the following discussion on how to avoid rigid and hard to maintain code: http://stackoverflow.com/questions/18673990/how-to-share-data-between-viewcontrollers-in-a-tabbed-application/18674772#18674772 – Joride Dec 29 '15 at 14:25