I created a singleton called “GCTurnBasedMatchHelper”, which includes the following code in the header:
@property (nonatomic, retain) NSString *pick1;
In the implementation I have these lines:
#import "GCTurnBasedMatchHelper.h"
//Some implementation code in here...
@synthesize pick1;
- (void) pick{
int r = arc4random() % 2;
if (r==0) {
pick1 =[[NSString alloc] initWithFormat:@"Askerer"];
NSLog(@"%@", pick1);
} else {
pick1 =[[NSString alloc] initWithFormat:@"Answerer"];
NSLog(@"%@", pick1);
}
}
How can I access the value of pick1 from another class implementation ViewController.m?
And how can I access other properties from ViewController.m in GCTurnBasedMatchHelper.m?
Thanks!
You can access the properties of your Singleton class from
ViewController.mby doing something such as this:Assuming,
-sharedInstanceis what the initialization method is for your Singleton, i.e:The tricker bit, is Accessing properties that’re in
ViewController.mfrom your Singleton. I’d recommend creating aData SourceorDelegatefor your Singleton that returns your desired properties.Assuming
-viewControllerPropertyis aDelegatemethod for your singleton. In your singleton, you can now accessViewControllerclass properties by invoking this from your singleton.Of course, I didn’t show creating and or setting the delegate, nor none of the other obvious stuff like @synthesizing. I take it you can figure this out, as it’s not in the scope of your question.