Problems with accessing Class Properties from another Class

Discussion in 'iOS Programming' started by mspatts, Oct 2, 2008.

  1. mspatts macrumors newbie

    Joined:
    Aug 28, 2008
    #1
    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]
    ...
    }
    
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. mspatts thread starter macrumors newbie

    Joined:
    Aug 28, 2008
    #3
    how do i fix this?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     

Share This Page