tags:

views:

143

answers:

2

I am trying to perform a simple operation that i have done alot in this program. I am trying to launch the CLLocationManager once the app is finished launching and get the current location once the update has happened. My app works in that it get the current location, when i NSLog the current location it works fine. The problem is that i am trying to pass this location from one object to another and it does not have a value once it gets there.

I have been looking up accessor methods and everything seems right as i have these types of operations all over my program. I have tried to follow numerous examples but none seem to work.

Here is some code.

This is the .h file of the CLLocationManagerDelegate.

@interface YogaAllianceAppDelegate : NSObject <UIApplicationDelegate,CLLocationManagerDelegate> {

CLLocationManager *locationManager;
    CLLocation *mycurrentLocation;

    float curLats;
    float curLons;
    BOOL locationDidUpdate;

    UIWindow *window;

    YogaAllianceViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;  
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, assign) float curLats;
@property (nonatomic, assign) float curLons;
@property (nonatomic, assign) CLLocation *mycurrentLocation;
@property (nonatomic, retain) IBOutlet YogaAllianceViewController *viewController;
@end

This is the .m file

@synthesize viewController, locationManager, curLats, curLons, mycurrentLocation;





- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    self.locationManager = [[[CLLocationManager alloc]init] autorelease];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.delegate = self;
    [locationManager startUpdatingLocation];


    locationDidUpdate = NO;
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

}


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

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"currentLocationDidUpdate" object: self];

    [locationManager stopUpdatingLocation];



    NSLog(@"Core Location says location available");


    self.curLats = newLocation.coordinate.latitude;
    self.curLons = newLocation.coordinate.longitude;

    self.mycurrentLocation  = newLocation;


    NSLog(@"current position is then %@", [mycurrentLocation description]);

}

At this point it works. I get the location printed out and everything, but i need that location in another object and it seems that all my attempts at reworking the dot notation has failed. Here is the . h of the object i need it in.

@interface School_Location_List_Controller : UIViewController {


    CLLocation* currentLocation; 

}

- (IBAction) btnBack_Clicked:(id)sender;


@property (nonatomic, assign) CLLocation* currentLocation;

And here is the .m

@implementation School_Location_List_Controller

@synthesize destinations, closeSchools, currentLocation;



-(void) viewWillAppear: (BOOL)animated {


    NSLog (@"Registered for location events");

    YogaAllianceAppDelegate *updated = [[YogaAllianceAppDelegate alloc]init];


    currentLocation = updated.mycurrentLocation ;



    NSLog(@"current position here in the passed object is %@", [currentLocation description]);


    [super viewWillAppear:animated];
}

It is at this point that i am confused. When i print that out, it just says the currentLocation is Null. Its like it never got the data..

Thank you guys in advance.

A: 

Your problem is this line:

YogaAllianceAppDelegate *updated = [[YogaAllianceAppDelegate alloc]init];

Here, you're allocating and initializing a brand new object, one that is entirely distinct from the application's existing delegate. If you want to get the application delegate that already exists, use:

YogaAllianceAppDelegate *updated = (YogaAllizanceAppDelegate *)[[UIApplication sharedApplication] delegate];
Dave DeLong
Oops! I didn't even catch that!
Jonathan Sterling
DAVE!!!! I cannot thank you enough.. I am new to this whole thing and have been racking my brain for days on that thing. Its always the simple things that do that.. Thank you
Makinitez21
A: 

I'm not sure if this is your problem, but try this: You have the mycurrentLocation property as assign, which means that when set, you just have a pointer to an object. When the location is released, mycurrentLocation can't hold on to it.

I suggest using retain or copy.

Jonathan Sterling
See @Dave Delong's answer above. His is probably correct. Sorry!
Jonathan Sterling
John, Thank you for your reply it was a help.
Makinitez21