How to pass objects between view controllers in Objective-C?

I made my way through some code for two days, trying to understand why I could not get the global variable NSMutableArray, declared in .h and implemented in .m, and set the viewDidLoad function.

It finally dawned on me: in Objective-C there is no such thing as a global variable, at least not in the sense of PHP that I learned about. I never read Xcode error warnings, but it was, even if not quite plain English: "Blah" instance variable, available in the class method.

My question is: what should I do now? I have two view controllers that need to access the central NSMutableDictionary, which I generate from the JSON file at the URL. This is basically an extended menu for all my abbreviations in Table View, and I would like to have a couple of other "global" (non-static) variables.

Do I need to pick up JSON every time I want to generate this NSMutableDictionary, or is there a way to set it once and access it from different classes via #import? Should I write data to a file, or is there another way people usually do this?

+5
source share
4 answers

If you have two view controllers that access a common NSMutableDictionary, can you pass a pointer to a common dictionary in the corresponding messages about their launch?

So in 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 implementations of UIViewControllerOne and UIViewControllerTwo

 - (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]; } 
+9
source

There are essentially many ways to do this (without resorting to something extreme, like writing to a file). You can create a "global" using:

  • Old fashioned global variables C (externs)
  • A Singleton Class
  • instance variables in application deletion

But all these approaches make your view controller less modular (because it depends on reaching outside to find global data), so the best approach is probably to make the dictionary one of the properties of the view controller class, which should be the explicitly set caller (either inside the initWithDictionary: method, or using a separate installer).

+2
source

There are global variables in Obj-C, you create them in the App Delegate.

But for your problem, you probably want to pass NSMutableDictonary when you create a new view controller, for example [UIView alloc] initWithDictionary: (NSMutableDictionary *) dict; if you know what I mean.

In your example, do you have one class calling another class? I would think that there is some kind of controller that determines which display controller should be displayed, and this will be the place to access and go through the dictionary

+1
source

Just create a global variable. Global means outside of any field.

 NSMutableDictionary *gDictionary; @implementation ... @end 
+1
source

All Articles