views:

29

answers:

2

I have a app that uses several view controllers. In one instance I need to uses an int (int lives) in a seperate view controller from where it is created. I have tried using it and it throws and error at build claiming "lives" undeclared. I am already importing the view controller where the int was declared. I am stuck on this one.

I would appreciate any help.

A: 

Use properties, or custom setter and getter for instanse variables.

jamapag
Thanks for the instant replies! Could you give me a quick example of how I might do this using an int?
Adam_D
- (int)getMyIntVariable { return myIntVariable; }
jamapag
A: 

You could use the application delegate to store information you need in different places in you app.

The proper way would be to implement your own delegate protocol. For a recent discussion on this topic, see pass a variable up the navigation stack

mvds
After looking around the app delegate looks like the place to do it. Do I just store variables in appdelegate.h and then does that make it global??
Adam_D
almost, you need to declare them, add a @property line, and add a @synthesize line in the `.m` file. Then you can access them using getters/setters, like `[appDelegate setMyValue:5];` where `addDelegate` should point to your app delegate.
mvds
Thanks for the help but I still get a couple of build warnings and runs fine until I call the get method. // ViewControllerDemoAppDelegate.hNSInteger *MyInt;@property (nonatomic) NSInteger *MyInt;// ViewControllerDemoAppDelegate.m@synthesize MyInt;MyInt = 5;[release MyInt];//levelpassed.m#import "ViewControllerDemoAppDelegate.h"fish = [ViewControllerDemoAppDelegate getMyInt];BW- 1) ViewControllerDemoAppDelegate may not respond to "+getMyInt"2) Assignment makes pointer from integer without a castShould I be using NSInteger? Am I using the properties correctly?
Adam_D
Not entirely correct: 1) declare it `NSInteger myInt` without `*`, 2) you should not call `getMyInt` but just `myInt`, and 3) you need to call it on the app delegate instance object, not the class. Easiest way for that is `[[[UIApplication sharedApplication] delegate] myInt]`
mvds