Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mspatts

macrumors newbie
Original poster
Aug 28, 2008
8
0
I'm trying to integrate the CLLocationManager (MyCLController) from Apple's LocateMe sample into my app, and having problems accessing its data from my main class (AppDelegate).

Within the MyCLController class, I have added a variable called "currLoc" which I update every time a location change is detected (not sure if this is the best way to do this). I'd like to be able to get the value of this variable from anywhere in my App as needed. Not sure if i missed if something like this is already built in.

When I attempt to get currLoc from AppDelegate, my app crashes... I just started and barely understand Objective C, so if you've got a fix, please dumb it down as much as possible. I've tried my best to understand Apple's The Objective-C 2.0 Programming Language sections on Classes/Properties, etc, but am having no luck.

Code excerpts below.
Code:
/******  File: MyCLController.h  ****/

// This protocol is used to send the text for location updates back to another view controller
@protocol MyCLControllerDelegate <NSObject>
@required
-(void)newLocationUpdate:(NSString *)text;
-(void)newError:(NSString *)text;
@end


// Class definition
@interface MyCLController : NSObject <CLLocationManagerDelegate> {
	CLLocationManager *locationManager;
	id delegate;
[B]	NSString *currLoc;[/B]
}

@property (nonatomic,retain) CLLocationManager *locationManager;
@property (nonatomic,assign) id <MyCLControllerDelegate> delegate;
[B]@property (readonly) NSString *currLoc;[/B]

- (void)locationManager:(CLLocationManager *)manager
	didUpdateToLocation:(CLLocation *)newLocation
		   fromLocation:(CLLocation *)oldLocation;

- (void)locationManager:(CLLocationManager *)manager
	   didFailWithError:(NSError *)error;

+ (MyCLController *)sharedInstance;

@end
Code:
/******  File: MyCLController.m  *****/

@implementation MyCLController

@synthesize locationManager;
[B]@synthesize currLoc;[/B]

// Called when the location is updated
- (void)locationManager:(CLLocationManager *)manager   didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
   ...
[B]   currLoc = [[NSString stringWithFormat:@"%@",newLocation] stringByReplacingOccurrencesOfString: @"@" withString: @","];
  // i had to replace the @ in newLocation as this was causing errors elsewhere
   NSLog(@"new currLoc: %@",currLoc);  // this works fine[/B]
   ...
}
Code:
/***** File: AppDelegate.m  *****/
- (void)applicationDidFinishLaunching:(UIApplication*)application {
...
// starts the gps
[B][[MyCLController sharedInstance].locationManager startUpdatingLocation];[/B]
// log the current location.. [B]this causes a crash![/B]
[B]NSLog(@"%@", [[MyCLController sharedInstance] currLoc];[/B]
...
}
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Your class does not have properties: an instance of the class does. You seem to be trying to use a singleton pattern (call to sharedInstance) but you do not seem to have written the code required to support this.
 

mspatts

macrumors newbie
Original poster
Aug 28, 2008
8
0
Your class does not have properties: an instance of the class does. You seem to be trying to use a singleton pattern (call to sharedInstance) but you do not seem to have written the code required to support this.

how do i fix this?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
how do i fix this?

Either write the required code or create an instance of the class and hold a reference to it. Either way you seem to have no handle on Object-Oriented programming at all and should probably back up and learn about that at a conceptual level.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.