删除先前的注释后,不会调用自定义注释标注气泡按钮方法

时间:2016-06-27 12:03:38

标签: ios objective-c mkmapview mkannotation mkpinannotationview

问题是删除注释会禁用rightCalloutAccessoryView的自定义注释按钮。

点击自定义按钮时,会调用协议方法mapView:annotationView:calloutAccessoryControlTapped,但不会调用自定义按钮的发送方法。首先添加注释以使地图工作正常,问题从第二个注释的初始化开始。

以下是代码:

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBarText {
      [searchBarText resignFirstResponder];
      [self.searchTable dismissViewControllerAnimated:YES completion:nil];

      CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //Geocoding search bar text (adress).
     [geocoder geocodeAddressString:searchBarText.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
         if (error) {
             NSLog(@"Error with geocoding: %@", error);
        } else {
             //To remove previous annotation from MapView using its reference.
           if (self.previousAnnotation != nil) {

             [self.mapView removeAnnotation:self.previousAnnotation];
            self.previousAnnotation = nil;

            //To invoke result as the placemark object.
            CLPlacemark *placemark = placemarks.firstObject;
            //Adjusted region setup and data.
            MKCoordinateRegion region = MKCoordinateRegionMake(placemark.location.coordinate, MKCoordinateSpanMake(0.01, 0.01));
            MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:region];
            [self.mapView setRegion:adjustedRegion animated:YES];

            CustomAnnotation *customAnnotation = [[CustomAnnotation alloc] initWithTitle:placemark.name andLocation:CLLocationCoordinate2DMake(region.center.latitude, region.center.longitude)];

            [self.mapView addAnnotation:customAnnotation];

            //To save reference of newly added annotation.
            self.previousAnnotation = customAnnotation;

        } else if (self.previousAnnotation == nil) {

            //To invoke result as the placemark object.
            CLPlacemark *placemark = placemarks.firstObject;
            //Adjusted region setup and data.
            MKCoordinateRegion region = MKCoordinateRegionMake(placemark.location.coordinate, MKCoordinateSpanMake(0.01, 0.01));
            MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:region];
            [self.mapView setRegion:adjustedRegion animated:YES];

            CustomAnnotation *customAnnotation = [[CustomAnnotation alloc] initWithTitle:placemark.name andLocation:CLLocationCoordinate2DMake(region.center.latitude, region.center.longitude)];

            [self.mapView addAnnotation:customAnnotation];

            //To save reference of newly added annotation.
            self.previousAnnotation = customAnnotation;
        }
    }
}];
}

逻辑似乎很好。如果我使用下面的代码初始化注释没有出现问题,我点击按钮并使用每个新注释调用按钮发送方法。

            //To invoke result as the placemark object.
            CLPlacemark *placemark = placemarks.firstObject;
            //Adjusted region setup and data.
            MKCoordinateRegion region = MKCoordinateRegionMake(placemark.location.coordinate, MKCoordinateSpanMake(0.01, 0.01));
            MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:region];
            [self.mapView setRegion:adjustedRegion animated:YES];

            CustomAnnotation *customAnnotation = [[CustomAnnotation alloc] initWithTitle:placemark.name andLocation:CLLocationCoordinate2DMake(region.center.latitude, region.center.longitude)];

            [self.mapView addAnnotation:customAnnotation];

似乎注释删除和新注释初始化之间存在问题,但我无法弄明白。

编辑(更多问题详情)

通过在地图上显示第二个注释,标注按钮不会将其称为方法(尽管检测到点击)。例如:

我搜索adress1,点击callout自定义按钮,调用自定义按钮方法。

我搜索adress2,正确删除了adress1标注气泡,地图上出现了正确坐标的新adress2标注气泡,但自定义按钮仍处于选定状态,点击它并不会调用其方法。

编辑2

删除注释是正确的(新版本替换旧版本,我通过记录self.mapView.annotations进行检查,我也检查了地标对象,新搜索结果是新位置。)

1 个答案:

答案 0 :(得分:0)

好的,我明白了。

问题在于删除注释后,注释视图在内部排队等待以后重用(根据MKMapView类引用)。您需要做的就是在致电dequeueReusableAnnotationViewWithIdentifier:后使用removeAnnotation:方法。

         if (self.previousAnnotation != nil) {

            [self.mapView removeAnnotation:self.previousAnnotation];
            //Set it nil before attaching it with new reference.
            self.previousAnnotation = nil;

            //To invoke result as the placemark object.
            CLPlacemark *placemark = placemarks.firstObject;

            //Adjusted region setup and data.
            MKCoordinateRegion region = MKCoordinateRegionMake(placemark.location.coordinate, MKCoordinateSpanMake(0.01, 0.01));
            MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:region];
            [self.mapView setRegion:adjustedRegion animated:YES];

            CustomAnnotation *customAnnotation = [[CustomAnnotation alloc] initWithTitle:placemark.name andLocation:CLLocationCoordinate2DMake(region.center.latitude, region.center.longitude)];

            [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"];
            [self.mapView addAnnotation:customAnnotation];

            //To save reference of newly added annotation.
            self.previousAnnotation = customAnnotation;