Variable memory management on iPhone
I know this is the main quest, but I just didn't find a good answer. I have an application with multiple view controllers and I noticed that if I create a variable and take action on it in one controller:
int foo = 0; foo =+ 1;
I can declare a variable in another controller without initializing its value, it will carry the value it was last set in the previous view controller:
int foo;
if (foo == 1)
doSomething;
I've used this to my advantage to keep track of the current player in a multiplayer game, etc ... using data in multiple controllers as their views are loaded and deleted. I'm new to Obj C and based on what I've read this doesn't seem like the right way to go about things.
So here's my question, is there a safe way to transfer data between controllers, and if not what should I do?
a source to share
This is not a secure way to transfer information between controllers. If you use this in your application and it works, I am really quite surprised.
The correct way to do this is to actually pass the value you want from one controller to another. In the example below, the application delegate stores a value and then each controller reads / writes to it ... this is just an example, be careful to use the application delegate as a repository in order to become efficient globals . Setting up a lot of variables in the application's deletion to use in this way will make your application difficult to maintain.
In Appate.h application
@interface AppDelegate : NSObject <UIApplicationDelegate> {
int sharedInt;
}
@property int sharedInt;
In your application Delegate.m
@synthesize sharedInt; // this will automatically create the getters and setters
Then in the controller where you want to set the value:
ApplicationDelegateClassNameHere * applicationDelegate = [[ UIApplication sharedApplication] delegate];
applicationDelegate.sharedInt = 3;
Then in the controller where you want to read the value:
ApplicationDelegateClassNameHere * applicationDelegate = [[ UIApplication sharedApplication] delegate];
if (applicationDelegate.sharedInt == 3) {
//do your stuff here
}
Edited : forgot @synthesize.
Edited : Typed a return from a one-dot delegate method based on Kenneth Ballenegger's suggestion to eliminate compiler warnings.
a source to share
I recommend doing (and doing) something similar to the mmc answer. However, his answer will cause you to have many warnings. You have to make sure that you are getting an object AppDelegate *
, not just any object ( id
) (for example, you do with the mmc method).
Just add a class method +singleton
(or +sharedDelegate
) to your delegate like this:
+ (iLaughAppDelegate *)singleton
{
iLaughAppDelegate *delegate = (iLaughAppDelegate *)[[UIApplication sharedApplication] delegate];
return delegate;
}
a source to share
This is fine if done correctly, but only needs to be done to read the data. This is an extremely common way to share string constants. For instance:
myobject.h
extern NSString* const MyObjectWillChangeNotification;
MyObject.m
NSString* const MyObjectWillChangeNotification = @"MyObjectWillChangeNotification";
AnotherObject.m
#import "MyObject.h"
// You can now use MyObjectWillChangeNotification in this file
This same process will technically work for yours int
, but you shouldn't, as previously noted. It is much safer to allow one and only one object to control any given variable. Everyone else needs to access it through an accessory.
a source to share