I've been trudging through some code for two days trying to figure out why I couldn't fetch a global NSMutableArray variable I declared in the .h and implemented in .m and set in a the viewDidLoad function.
It finally dawned on me: there's no such thing as a global variable in Objective-C, at least not in the PHP sense I've come to know. I didn't ever really read the XCode error warnings, but there it was, even if not quite plain English: "Instance variable 'blah' accessed in class method."
My question: What am I supposed to do now? I've got two View Controllers that need to access a central NSMutableDictionary I generate from a JSON file via URL. It's basically an extended menu for all my Table View drill downs, and I'd like to have couple other "global" (non-static) variables.
Do I have to grab the JSON each time I want to generate this NSMutableDictionary or is there some way to set it once and access it from various classes via #import? Do I have to write data to a file, or is there another way people usually do this?
If you have a value in one view controller and want to pass it to another, there are two approaches: for passing data forward you should communicate using properties, and for passing data backwards you can either use a delegate or a block.
Using segues in your storyboard is the recommended way to present and dismiss view controllers. A segue is a visual representation of a transition from one view controller to another. A segue starts with an action such as a button tap or table-row selection in the initial view controller.
If you have two view controllers that access the shared NSMutableDictionary, can you pass a pointer to the common dictionary into their respective init messages?
So in your AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// the app delegate doesn't keep a reference this, it just passes it to the
// view controllers who retain the data (and it goes away when both have been released)
NSMutableDictionary * commonData = [[NSMutableDictionary new] autorelease];
// some method to parse the JSON and build the dictionary
[self populateDataFromJSON:commonData];
// each view controller retains the pointer to NSMutableDictionary (and releases it on dealloc)
self.m_viewControllerOne = [[[UIViewControllerOne alloc] initWithData:commonData] autorelease];
self.m_viewControllerTwo = [[[UIViewControllerTwo alloc] initWithData:commonData] autorelease];
}
And in your respective UIViewControllerOne and UIViewControllerTwo implementations
- (id)initWithData:(NSMutableDictionary*)data
{
// call the base class ini
if (!(self=[super init]))
return nil;
// set your retained property
self.sharedData = data;
}
// don't forget to release the property
- (void)dealloc {
[sharedData release];
[super dealloc];
}
There are, in fact, a bunch of ways you can do this (without resorting to something extreme like writing to a file). You can create a "global" by using:
But all of these approaches make your view controller less modular (because they depend on reaching "outside" to find the global data), so the best approach is probably to make the dictionary a property of the view controller class that must be explicitly set by the caller (either within an initWithDictionary: method, or with a separate setter).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With