title: iPhone MapKit problems: viewForAnnotation inconsistently setting pinColor?
body: <p>I'm trying setup a map that displays different pin colors depending on the type/class of the location in question.  I know this is a pretty common thing to do, but I'm having trouble getting the viewForAnnotation delegate to consistently update/set the pin color.<br>
    I have a showThisLocation function that basically cycles through a list of AddressAnnotations and then based on the annotation class (bus stop, hospital, etc.) I set an </p>
  
  <pre><code>
  if( myClass == 1){
   [defaults setObject:@"1" forKey:@"currPinColor"];
   [defaults synchronize];
   NSLog(@"Should be %@!", [defaults objectForKey:@"currPinColor"]);
  } else if( myClass ==2 ){
   [defaults setObject:@"2" forKey:@"currPinColor"];
   [defaults synchronize];
   NSLog(@"Should be %@!", [defaults objectForKey:@"currPinColor"]);
  }
  [_mapView addAnnotation:myCurrentAnnotation];
  
  </code></pre>
  
  <p>then my viewForAnnotation delegate looks like this,</p>
  
  <pre><code>
  - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation {
      if( annotation == mapView.userLocation ){ return nil; }
  
      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
      MKPinAnnotationView *annView = nil;
      annView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"currentloc"];
      if( annView == nil ){
          annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
      }
  
      annView.pinColor =  [defaults integerForKey:@"currPinColor"];
      NSLog(@"Pin color: %d", [defaults integerForKey:@"currPinColor"]);
      annView.animatesDrop=TRUE;
      annView.canShowCallout = YES;
      annView.calloutOffset = CGPointMake(-5, 5);
      return annView;
  }
  </code></pre>
  
  <p>The problem is that, although the NSLog statements in the "if" block always confirm that the color has been set, the delegate sometimes but not always ends up with the correct color.  I've also noticed that what generally happens is that the first search for a new location will set all pins to the last color in the "if" block, but search for the same location again will set the pins to the correct color.  </p>
  
  <p>I suspect I am not supposed to usen NSUserDefaults in this way, but I also tried to create my own subclass for MKAnnotation which included an additional property "currentPinColor", and while this allowed me to set the "currentPinColor", when I tried to access the "currentPinColor from the delegate method, the compiler complained that it didn't know anything about "currentPinColor in connection with MKAnnotation.  Fair enough I guess, but then I tried to revise the delegate method,</p>
  
  <pre><code>
  - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id &lt;MyCustomMKAnnotation&gt;)annotation 
  </code></pre>
  
  <p>instead of the default</p>
  
  <pre><code>
  - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation 
  </code></pre>
  
  <p>at which point the compiler complained that it didn't know anything about the protocol for MyCustomMKAnnotation in this delegate context.</p>
  
  <p>What is the proper way to set the delegate method and/or MyCustomMKAnnotation, or what is the appropriate way to achieve consistent pinColor settings.  I'm just about out of ideas for things to try here.</p>