I'm trying to use the singleton concept to retain GPS information, but I do no see where is the problem my variable display just zeroes.
Singleton.h:
@interface Singleton : NSObject
@property (readwrite) NSString *propertyA;
@property (readwrite) NSString *propertyB;
@property (readwrite) NSString *propertyC;
+ (Singleton *)sharedInstance;
@end
Singleton.m:
#import "Singleton.h"
@interface Singleton ()
{
NSString *variableA;
NSString *variableB;
NSString *variableC;
}
@end
@implementation Singleton
static Singleton *sharedInstance = nil;
+ (Singleton *)sharedInstance
{
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [[Singleton alloc] init];
}
}
return sharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [super allocWithZone:zone];
return sharedInstance;
}
}
return nil;
}
- (id)init
{
self = [super init];
if (self)
{
variableA = nil;
variableB = nil;
variableC = nil;
}
return self;
}
@end
in OverlayViewController.m:
#import "Singleton.h"
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
_longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
_latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
// Setting properties
NSString *variableA = _latitudeLabel.text;
[[Singleton sharedInstance] setPropertyA:variableA];
}
Then I recall the value a bit further down ...
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *variableA = [[Singleton sharedInstance] propertyA];
NSLog(@"Latitude = %.8f", variableA);
}
Thanks Regis