Customize contact card color dynamically for iOS

I am parsing an xml that contains lines 0, 1, and 2.

// For reference 0 = green 1 = red 2 = Purple

I have a class that confirms MKAnnotaion, which contains the following variables, which are properties.

CLLocationCoordinate2D  coordinate;
NSString            *title;
NSString            *subtitle;
MKPinAnnotationColor pinColor;

This class is called MyAnnotation.

Now, in the map display viewDidLoad, I run a for loop to iterate over the analyzed data as shown below (locationArray stores this data, and I completely retrieve all the information.

 for (int i = 0; i < locationArray.count; i++) {
    myAnnotation =[[MyAnnotation alloc] init];

    NSString *thePointName = [[locationArray objectAtIndex:i]xmlName];
    NSString *theAddress = [[locationArray objectAtIndex:i]xmlAddress];

    NSString *latString = [[locationArray objectAtIndex:i]xmlLat];
    NSString *lonString = [[locationArray objectAtIndex:i]xmlLon];

// This is a string that extends the mentioned 0, 1 or 2 lines, which represent the color of poinType contacts, saved as a string

    pointType = [[locationArray objectAtIndex:i]xmlType];

    double theLatitude = [latString doubleValue];
    double theLongtitude = [lonString doubleValue];

    userLocation.latitude=theLatitude;
    userLocation.longitude=theLongtitude;

    myAnnotation.coordinate=userLocation;
    myAnnotation.title=[NSString stringWithFormat:@"%@", thePointName];
    myAnnotation.subtitle=[NSString stringWithFormat:@"%@", theAddress];

    //I log that the points are actually giving either of the colors and if so set MyAnnotation class to the pincolor 

    NSLog(@"Points Color %@", pointType);
    if ([pointType isEqualToString:@"0"]){
        myAnnotation.pinColor = MKPinAnnotationColorGreen;
    }else if ([pointType isEqualToString:@"1"]){
        myAnnotation.pinColor = MKPinAnnotationColorRed;
    }else if ([pointType isEqualToString:@"2"]) {
        myAnnotation.pinColor = MKPinAnnotationColorPurple;
    }

    [mapView addAnnotation:myAnnotation];

    }

Now in the MKAnnotationView I am doing below

  - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id     <MKAnnotation>)annotation

 {

// if it the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
                                  initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;

    //set pin color to the correct colour
    if (myAnnotation.pinColor = MKPinAnnotationColorGreen) {
    pinView.pinColor = MKPinAnnotationColorGreen;
    }

    else if (myAnnotation.pinColor = MKPinAnnotationColorRed) {
    pinView.pinColor = MKPinAnnotationColorRed;

     }

    else if (myAnnotation.pinColor = MKPinAnnotationColorPurple){
    pinView.pinColor = MKPinAnnotationColorPurple;
    }



    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
                action:@selector(showDetails:)
      forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;


    UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"Profile.png"]];
pinView.leftCalloutAccessoryView = profileIconView;

return pinView;
  }

I also tried in the method described above, however it does not set the colors of the pin. Everything else is fine!

    //set pin color to the correct colour
    if (pointType isEqualToString:@"0") {
    pinView.pinColor = MKPinAnnotationColorGreen;
    }

    else if (pointType isEqualToString:@"1") {
    pinView.pinColor = MKPinAnnotationColorRed;

    }

    else if (pointType isEqualToString:@"2"){
    pinView.pinColor = MKPinAnnotationColorPurple;
    }
+2
1

viewForAnnotation:

if (myAnnotation.pinColor = MKPinAnnotationColorGreen) {

:

  • , , . . , № 2...

  • myAnnotation, , . , for, myAnnotation. , viewForAnnotation addAnnotation. , , .

annotation, . , .

annotation id<MKAnnotation>, , :

//first make sure this annotation is our custom class before casting it...
if ([annotation isKindOfClass:[MyAnnotation class]])
{
    MyAnnotation *currentAnn = (MyAnnotation *)annotation;

    if (currentAnn.pinColor == MKPinAnnotationColorGreen) {
        pinView.pinColor = MKPinAnnotationColorGreen;
    }

    else if (currentAnn.pinColor == MKPinAnnotationColorRed) {
        pinView.pinColor = MKPinAnnotationColorRed;

    }

    else if (currentAnn.pinColor == MKPinAnnotationColorPurple) {
        pinView.pinColor = MKPinAnnotationColorPurple;
    }
}

:

//first make sure this annotation is our custom class before casting it...
if ([annotation isKindOfClass:[MyAnnotation class]])
{
    MyAnnotation *currentAnn = (MyAnnotation *)annotation;        
    pinView.pinColor = currentAnn.pinColor;
}

(, rightButton, ?)

+3

Source: https://habr.com/ru/post/1547441/


All Articles