如何从这个方法返回变量?

时间:2013-08-20 09:42:53

标签: objective-c methods

我需要从此方法返回经度纬度。这来自locationManager代码。当我记录lat时,它们会给出正确的值。

我无法做的是让你从这个方法中导出/返回变量。我该如何改变呢?

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

    int degrees = newLocation.coordinate.latitude;
    double decimal = fabs(newLocation.coordinate.latitude - degrees);
    int minutes = decimal * 60;
    double seconds = decimal * 3600 - minutes * 60;
    self.lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", degrees, minutes, seconds];
    NSLog(@"Current Latitude : %@",self.lat);

    degrees = newLocation.coordinate.longitude;
    decimal = fabs(newLocation.coordinate.longitude - degrees);
    minutes = decimal * 60;
    seconds = decimal * 3600 - minutes * 60;

    self.longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", degrees, minutes, seconds];
    NSLog(@"Current Longitude : %@",self.longt);
    [manager stopUpdatingLocation];
}

1 个答案:

答案 0 :(得分:1)

您不需要返回该方法的位置,因为您不打算自己调用该方法 - 位置管理器会调用该方法为您提供更新的位置。您应该做的是存储新位置,以便您的代码可以在以后使用它。通常,这只是意味着更新您的实例变量,但您可以使用该位置执行任何操作 - 更新数据模型,将其写入文本文件等。将其存储在ivars中可以随时轻松访问该位置在你的代码中需要它。

相关问题