Search code examples
iosuibuttonuiimagemkannotationmkpinannotationview

Why when I press the AnnotationView sometime the image disappear?


In my MapView there are multiple annotationView, each with their own image, it happens that sometimes when I press the image, it disappears and never comes back if not reloading the viewController. Why does this happen?

 - (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id 
    <MKAnnotation>)annotation
    {
         if ([annotation isKindOfClass:[AnnotationView class]]) {

               return nil;
         }

         if ([annotation isKindOfClass:[AnnotationCustom class]]){


            static NSString* AnnotationIdentifier = @"AnnotationIdentifier";

           MKPinAnnotationView *annotationView = (MKPinAnnotationView*) [mapView 
                   dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];

           annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation

                             reuseIdentifier:AnnotationIdentifier];

        if ([self.nameTable isEqualToString:@"Movies"]){
            if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){

                  annotationView.image = [UIImage imageNamed:@"iphone_movies.png"];


            }else{
                  annotationView.image = [UIImage imageNamed:@"ipad_movies.png"];

            }


     }else if ([self.nameTable isEqualToString:@"Food_Drink"]){
             if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
                 annotationView.image = [UIImage imageNamed:@"iphone_foodDrink.png"];


             }else{
                 annotationView.image = [UIImage imageNamed:@"ipad_foodDrink.png"];


             }
     }else if (......){
              //same procedure for all annotationView

     }


    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    annotationView.rightCalloutAccessoryView=detailButton;

    annotationView.canShowCallout = YES;
    annotationView.draggable = YES;

    return annotationView;
    }

Something is missing?


Solution

  • I replaced

     MKPinAnnotationView *annotationView = (MKPinAnnotationView*) [mapView 
                   dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    
     annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
     reuseIdentifier:AnnotationIdentifier];
    

    with

      MKAnnotationView *annotationView = (MKAnnotationView*) [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
      annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];