在委托方法中获取值而不是在该方法的一侧

时间:2012-10-18 09:26:22

标签: objective-c mapkit

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{

    //[_mapview setMapType:MKMapTypeStandard];
    CLLocationCoordinate2D center = _mapview.centerCoordinate;
    double lat = center.latitude;
    double lng = center.longitude;
    NSString *str = [NSString stringWithFormat:@"%f",lat];
    NSString *str1 = [NSString stringWithFormat:@"%f",lng];
    _newlat = str;
    _newlong=str1;

    NSLog(@"%@ %@",_newlat,_newlong);
}

我在这方面获得了很长的lat值,但是没有在这个委托方法的一边。当我跳到上一课时,我希望在上课中有这个值。

我做了声明委托,但每次我拖动地图都会被调用,而且我没有在上一课中获得价值。

1 个答案:

答案 0 :(得分:0)

据我所知,您需要设置观察坐标的观察者对象,并在新坐标到来时通知。

在CLLocationManagerDelegate类.m

-(void) setLocationObserver:(id) observer
{
   self.locationObserver = observer
}

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{

    //[_mapview setMapType:MKMapTypeStandard];
    CLLocationCoordinate2D center = _mapview.centerCoordinate;
    double lat = center.latitude;
    double lng = center.longitude;
    NSString *str = [NSString stringWithFormat:@"%f",lat];
    NSString *str1 = [NSString stringWithFormat:@"%f",lng];
    _newlat = str;
    _newlong=str1;

    if ([self.locationObserver respondsToSelector:@selector(setNewCoords:)
        [self.locationObserver setNewCoords:center];

    NSLog(@"%@ %@",_newlat,_newlong);
}

在上一课.m:

-(void)-(void) setNewCoords:(CLLocationCoordinate2D*) location
{
    self.myLocation = location;
    [self doSeomethingWithNewLocation]; // if you wish
}

-(void) doSomethingWithNewLocation
{
    NSString * _newlat = [NSString stringWithFormat:@"%f",self.myLocation.latitude];
    NSString * _newlong = [NSString stringWithFormat:@"%f",self.myLocation.longitude];
}

粗略的例子,但可能有帮助。