I'm confused. Read up on a whole lot of resources but it didn't help. How can I pass a value generated in a method (variable) to another method so it can be used for calculations. I found a post about putting a property in the .h file. Did that. In my method I called first the methodname and then the variable but it will not work. This is the code:
-(void)recordLocation{
double valueLat = [[latitude text]doubleValue];
int latSeconds = valueLat * 3600;
int latDegrees = latSeconds / 3600;
latSeconds = abs(latSeconds % 3600);
int latMinutes = latSeconds / 60;
latSeconds %= 60;
char latChar = (latitude > 0) ? 'N' : 'S';
double valueLon = [[longitude text]doubleValue];
int lonSeconds = valueLon * 3600;
int lonDegrees = lonSeconds / 3600;
lonSeconds = abs(lonSeconds % 3600);
int lonMinutes = lonSeconds / 60;
lonSeconds %= 60;
char lonChar = (longitude > 0) ? 'E' : 'W';
CLLocation *tempPos = [[CLLocation alloc] initWithLatitude:valueLat longitude:valueLon]; //tempPos is what I want to pass
NSString *recPosition = [[NSString alloc]initWithFormat:@"%i° %i' %i\" %c" " " @"%i° %i' %i\" %c \n", latDegrees, latMinutes, latSeconds, latChar,lonDegrees, lonMinutes, lonSeconds, lonChar];
_labelDegrees.text = recPosition;
//NSLog(@"%f" , valueLat);
//NSLog(@"%f" , valueLon);
//NSLog (@"%@", tempPos);
}
- (CLLocationDistance)distanceFromLocation{
double lat1 = [[latitude text] doubleValue];
double lon1 = [[longitude text] doubleValue];
CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lon1];
CLLocationDistance locDistance = [currentLocation distanceFromLocation:recordLocation.tempPos];//tempPos is what I want to use from recordLocation
NSString *distText = [[NSString alloc]initWithFormat:@"%f", locDistance];
//NSLog(@"%f", lat1);
//NSLog(@"%f", lon1);
NSLog(@"%@", currentLocation);
NSLog(@"%@", tempPos);
[lonDistance setText:distText];
}
It is the variable tempPos what gives me pain. To be as complete as possible, here is my .h file.
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import "Corelocation/Corelocation.h"
NSString *latr;
NSString *longr;
NSString *latLabDeg;
NSString *lat;
NSString *lon;
NSString *acc;
double valueLon;
double valueLat;
double latvalue;
double lonvalue;
CLLocation *tempPos;
@interface ViewController : UIViewController <CLLocationManagerDelegate>
@property (strong, nonatomic) IBOutlet UITextField *accuracy;
@property (strong, nonatomic) IBOutlet UITextField *latitude;
@property (strong, nonatomic) IBOutlet UITextField *longitude;
@property (strong, nonatomic) CLLocationManager *location;
@property (strong, nonatomic) IBOutlet UITextField *latrec;
@property (strong, nonatomic) IBOutlet UITextField *longrec;
@property (strong, nonatomic) IBOutlet UILabel *labelDegrees;
@property (strong, nonatomic) IBOutlet UITextField *lonDistance;
@property (nonatomic) CLLocationDistance *locDistance;
@property (retain) CLLocation *tempPos;
//- (CLLocationDistance)distanceFromLocation;
Please, can you help me? Right now I feel very stupid because it has to be a simple thing.
Regards,
Adri
The reason of your problem is that in your method you are declaring a local variable so you are not working with your instance property.
CLLocation *tempPos = ...
Change that line to this:
tempPos = ...
EDIT: Some tips if you want to get that result from outside the method, there are two simple ways:
1) Return that variable. Since your method is a - (void)...
(isn't returning anything) you could easily add a return variable and return tempPos;
at the end
2) If in some future you already have a return value but you still want the outside method to have access to what you generated there, you can do this:
- (NSInteger)someMethod:(ObjectClassName **)object { // Yes, there are TWO of these > *
.. work some magic
ObjectClassName *theObject = ...
*object = theObject;
return whatever;
}
And you call it like this:
ObjectClassName *object = nil;
NSInteger whatever = [self someMethod:&object]; // < this behavior comes from c++
Or if you don't care about that variable, like this:
NSInteger whatever = [self someMethod:nil];