How to Pass Data Between UIViewControllers: The Global Variable & The Singleton Class

GLOBAL VARIABLES

The simple, but crude way is to use Global Variables. Global Variables can get messy because once you use a certain name as global variable, you can not use it again at ANY OTHER PLACES. And this can get confusing when you have many lines of codes and more than one class.

A quick example of Global Variable Declaration is as follows:

In your header (.h) file, input the “allocation” of the variable.

  1.  
  2. #import <UIKit/UIKit.h>;
  3. extern BOOL MYGlobalVariable;
  4. @interface BattleGameViewController : UIViewController
  5. @end;

“extern” does not declare it. What it does is just “allocate” a memory for that particular variable. Therefore to declare it, one need to declare in the implementation file (*.m) as follows:

  1. #import “MyAppHeader.h”
  2. @implementation BattleGameViewController
  3. BOOL MYGlobalVariable = NO;

So to use this variable in another class, all you need to do is import “MyAppHeader.h” and MYGlobalVariable should be ready to be used.

It works well, in fact, I also used it in some of my early apps. That is until I learn about Singleton.

SINGLETON

Believe it or not, MOST of us NOOBIES already used Singleton. The most common Singleton we use is…. *drumroll*… NSUserDefaults!

Do you like NSUserDefaults? I do. It is so easy and simple. And that is why you also should use Singleton in variable aspects of your app. It will make your coding very nice and easy to maintain.

How to implement a Singleton? I am no ObjC Guru, that much I can tell you, but hey, why reinvent the wheel, when some gurus have created the Singleton class for you? There are loadsssss of ways to write a Singleton Class, and you can find them here:

StackOverflow: Samples of Singletons

Anyway, here is MY singleton class. I used this Singleton class in one of my latest app – Clock Stand for iPad. I stripped off all the other variables and leave just 1 for example so that it is clear for you to see how to implement it.

  1. //
  2. //  MySingletonCenter.h
  3. //  ClockForiPad
  4. //
  5. //  Created by Emir Fithri Samsuddin on 6/20/12.
  6. //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @interface MySingletonCenter : NSObject {
  10. // Your variables go here
  11. // here’s one example:
  12.   BOOL is24Hour;
  13. }
  14. // Your property settings for your variables go here
  15. // here’s one example:
  16. @property (nonatomic, assign) BOOL is24Hour;
  17. // This is the method to access this Singleton class
  18. + (MySingletonCenter *)sharedSingleton;
  19. @end

And the implementation file (*.m)

  1. //
  2. //  MySingletonCenter.m
  3. //  ClockForiPad
  4. //
  5. //  Created by Emir Fithri Samsuddin on 6/20/12.
  6. //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  7. //
  8. #import “MySingletonCenter.h”
  9. @implementation MySingletonCenter
  10. static MySingletonCenter *shared = NULL;
  11. @synthesize is24Hour;
  12. – (id)init
  13. {
  14.     if ( self = [super init] )
  15.     {
  16.        // init values 
  17.        // here you assign initial values to your variable.
  18.        // in my case, I save all these values into NSUserDefaults as users preference.
  19.        // so i do the necessary things to ensure that happens.
  20.         NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
  21.         if ([def objectForKey:@“is24Hour”]==nil) {
  22.             is24Hour = NO;
  23.             [def setBool:self.is24Hour forKey:@“is24Hour”];
  24.             [def synchronize];
  25.         } else {
  26.             // read from NSUserDefaults
  27.             self.is24Hour = [def boolForKey:@“is24Hour”];
  28.         }
  29.     }
  30.     return self;
  31. }
  32. + (MySingletonCenter *)sharedSingleton
  33. {
  34.     @synchronized(shared)
  35.     {
  36.         if ( !shared || shared == NULL )
  37.         {
  38.             // allocate the shared instance, because it hasn’t been done yet
  39.             shared = [[MySingletonCenter alloc] init];
  40.         }
  41.         return shared;
  42.     }
  43. }
  44. – (void)dealloc
  45. {
  46.     NSLog(@“Deallocating singleton…”);
  47.     [super dealloc];
  48. }
  49. @end

Now, to create the Singleton Class, all you need to do is Right Click on the left panel of XCode and Choose Add File… and Choose Objective-C Class. Give a proper name (for eg, MySingletonCenter.h and MySingletonCenter.m) to it and add to project. After that just copy and paste the above Singleton code into the corresponding .h and .m. And you’re set to go.

To use Singleton in another class is easy.. in fact super easy just like NSUserDefaults.

1. #import “MySingletonCenter.h”

2. Everytime you want to read or write to it:

  1. MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
  2. // write
  3. tmp.is24Hour = YES;
  4. // read
  5. BOOL new = tmp.is24Hour;

 

 

Reference: http://xcodenoobies.blogspot.com/2012/08/how-to-pass-data-between.html

Leave a comment