Created
October 3, 2012 08:17
-
-
Save ryanhanwu/3825767 to your computer and use it in GitHub Desktop.
Pass value by singleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| +(GeneralData *) sharedInstance; | |
| @property (nonatomic) User *user; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static GeneralData __strong *generalDataObject = nil; | |
| @synthesize user; | |
| +(GeneralData *) sharedInstance | |
| { | |
| static dispatch_once_t pred; | |
| dispatch_once(&pred, ^{ | |
| generalDataObject = [[self alloc]init]; | |
| }); | |
| return generalDataObject; | |
| } | |
| //Singleton Reference : http://goo.gl/1KBHH | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "GeneralData.h"; | |
| - (IBAction)passValueButton:(id)sender { //An action method | |
| { | |
| User *user = [[User alloc] init]; | |
| user.name = "Ryan"; | |
| user.age = "26"; | |
| [GeneralData sharedInstance].user = user; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "GeneralData.h"; | |
| - (void)viewDidLoad | |
| { | |
| [super viewDidLoad]; | |
| User *currentUser = [GeneralData sharedInstance].user; | |
| NSLog(@"Name : %@", currentUser.name); | |
| NSLog(@"Age : %@", currentUser.age); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment