Question

I am having problems trying to pass a double value number to another viewController.

This is the NSNumber declaration on the receiving viewController:

@property (nonatomic, assign) NSNumber *lat;

And this is how am I getting the value from the source viewController:

double lat1 = [[[categorias objectAtIndex:i]objectForKey:@"latitud"] doubleValue];
double lon1 = [[[categorias objectAtIndex:i]objectForKey:@"longitud"] doubleValue];
annotation3.lat = [NSNumber numberWithDouble:lat1];
annotation3.lon = [NSNumber numberWithDouble:lon1];
NSLog(@"ANNOTATION LAT %@",annotation3.lat);//Log OK
NSLog(@"ANNOTAION LON %@",annotation3.lon);//Log OK

And then on another method, where I try to pass the value lat to the other viewController:

myAnnotation *ann = (myAnnotation *)view.annotation;
detailViewController.lat = ann.lat;

How should I format 'ann.lat'? I have tried

ann.lat

[ann.lat doubleValue]

[NSNumber numberWithDouble:ann.lat]

And always getting error.

Was it helpful?

Solution

As you are declaring your property as (assign) in your annotation class and I assume that its initial value was assigned from a local variable in a method in that class there is a very good chance that once your detailViewController tries to access the property it has been released because assigning the value to the property doesn't increase the object's reference count. This will result in an access violation exception. Change your property definition in your annotation to

@property  (strong,nonatomic) NSNumber *lat;

and see if that helps

OTHER TIPS

Since the detailViewController.lat is an NSNumber, you need to assign it an NSNumber. Since it is the same in the source view controller, all you need to do is a simple assign, the way that you did in your example:

detailViewController.lat = ann.lat;

The other two choices would be useful in these situations:

  • If the receiving view controller has a double and the source view controller has an NSNumber, you use [ann.lat doubleValue]
  • If the receiving view controller has an NSNumber and the source view controller has a double, you use [NSNumber numberWithDouble:ann.lat]

Logging this NSLog(@"ANN.LAT VALUE = %@", ann.lat); the app crashes on this line

This means that ann.lat is not an NSNumber, so the second situation described above applies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top