0

I want to send longitude and latitudevalues from the FirstViewControllerto another view controller but when passing the variables, their values are set to null and I had an error message : message sent to deallocated instance, here is my code: FirstViewController

NSString *nomDClient, *lat, *longt;
int degrees;
double decimal;
int minutes;
double seconds;

- (void)viewDidLoad {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    degrees = newLocation.coordinate.latitude;
    decimal = fabs(newLocation.coordinate.latitude - degrees);
    minutes = decimal * 60;
    seconds = decimal * 3600 - minutes * 60;
    lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",degrees, minutes, seconds];
    degrees = newLocation.coordinate.longitude;
    decimal = fabs(newLocation.coordinate.longitude - degrees);
    minutes = decimal * 60;
    seconds = decimal * 3600 - minutes * 60;
    longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"",  degrees, minutes, seconds];
    NSLog (@"%@Longitude", longt); // here I can get longitude value correctly
}

- (IBAction)pushSecondView:(id)sender {
    MC_AutourDeMoi *controller = [[MC_AutourDeMoi alloc] initWithNibName:@"MC_AutourDeMoi" bundle:nil];
    controller.longitudeUser = longt;
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)dealloc {
    [btn1 release];
    [locationManager release];
    [super dealloc];
}
modusCell
  • 13,151
  • 9
  • 53
  • 80
  • For starters, it would help you debug, and help this community to help, if you boiled this down to a minimum example: No location code, just a button that tries to push the vc, passing whatever longt is. (Looks like a NSString). Then post properly formatted code here. – danh Jul 18 '14 at 14:31
  • Looks like you're not using ARC (there are calls to release). So longt must be getting autoreleased at the end of the didUpdateToLocation method. Make it a property (copy attribute) like longitudeUser probably is. Also, instead of passing as strings, it would be better to pass as a simple CLLocationCoordinate2D struct to avoid worrying about memory management (and worrying about parsing strings). –  Jul 18 '14 at 14:36
  • Thank you for replying. When I pass a simple variable ( `longitude = @"str";` ) the code works well, the problem is in passing `longitude`and `latitude`variables coming from the location code – user2389273 Jul 18 '14 at 14:39
  • @user2389273 - do you set it to a dummy value in the location code, and see it work? (vs in the pushSecondView method). Also, if you paste the complete error message, there will be lots of hints in there. – danh Jul 18 '14 at 14:46
  • I tried to use `longt`as a property but it doesn't fix the problem – user2389273 Jul 18 '14 at 14:47
  • @Anna - agree that it's apt to be a zombie, but longt wouldn't be released in between initializing it and the NSLog right after, and if you made it a property, why would you suggest 'copy' instead of 'retain'? – danh Jul 18 '14 at 14:47
  • And when I do `NSLog (@"%@",long); in the `pushSecondView` Method, I get null value – user2389273 Jul 18 '14 at 14:49
  • @danh, For copy/retain, see http://stackoverflow.com/questions/387959/nsstring-property-copy-or-retain. I mean it gets autoreleased at the end of the method _after_ the NSLog (assuming that's the last statement in the method). –  Jul 18 '14 at 14:51
  • @user2389273, When making it a property, be sure to set its value using `self.longt = ...` not `longt = ...`. –  Jul 18 '14 at 14:53
  • @Anna - I see what you mean, but retain is fine here, if the longt were a retained property, the code would say 'self.longt = [NSString stringWithFormat:...', there'd be no other instance around to be mutated. Despite the giant up-voting on that post, I think the "you should always use copy" language is overstated. Besides, longt is not mutable. – danh Jul 18 '14 at 15:00
  • @user2389273 - would be happy to help with this one, but the question is not answerable in it's current form. Need the error message, need the results of some experimentation you've done, need all the extra code cut out (which would be part of the experiments). – danh Jul 18 '14 at 15:10
  • Can you show me how you are initializing the FirstViewController? The way you are initializing the FirstViewController has got something to do with this bug. – Srinivasan N Jul 18 '14 at 15:13
  • How can I get the value of `Longitude`and `Latitude` out of the function `- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation`? – user2389273 Jul 18 '14 at 16:46
  • @user2389273, Actually, you don't really need to copy the latitude and longitude "out of the function" because the locationManager has a `location` property which you can access in pushSecondView using `locationManager.location.coordinate`. –  Jul 18 '14 at 17:06
  • @Anna Thank you, your last comment was very helpful – user2389273 Jul 21 '14 at 10:09

1 Answers1

0

Why don't you try to call a method to set the properties such as in you SecondViewController try to Define a method like this :

in SecondViewController.h Declare two properties as Lattitude and Longitude

-(void)setlattitude:(NSString *)latt withLongitude:(NSString *)long;

In SecondViewController.m

define it as

-(void)setlattitude:(NSString *)latt withLongitude:(NSString *)long
{

Lattitude=latt;
Longitude= long;

}

Now when you are calling your secondViewController just do like this

SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[controller setlattitude:latitude withLongitude:longitude];
[self.navigationController pushViewController:controller animated:YES];
Geet
  • 2,427
  • 2
  • 21
  • 39