0

I want to set the tag in DetailDisclosure button in objective c. My old code is:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if (annotation == mapView.userLocation)
    {
        return nil;
    }
    else
    {
        static NSString * const identifier = @"MyCustomAnnotation";

        static int i;

        MKAnnotationView* annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if (annotationView)
        {
            annotationView.annotation = annotation;
        }
        else
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                  reuseIdentifier:identifier];
        }

        NSLog(@"%i",annotationView.tag);
        annotationView.tag=annotationView.tag+1;
        NSLog(@"%i",annotationView.tag);

        annotationView.image = [UIImage imageNamed:@"pin1.png"];
        annotationView.canShowCallout = YES;
        UIImageView *sfIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"newlocation.png"]];
        annotationView.leftCalloutAccessoryView = sfIconView;
        UIButton *InformationButton=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        [InformationButton addTarget:self action:@selector(annotationButtClicked:) forControlEvents:UIControlEventTouchUpInside];

        InformationButton.tag=i;

        annotationView.rightCalloutAccessoryView=InformationButton;

        UILongPressGestureRecognizer *longGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(LongPressEvent:)];

        [annotationView addGestureRecognizer:longGesture];

        return annotationView;
    }
}

Function Implementation

- (void)annotationButtClicked:(id)sender
{
    NSLog(@"%i",[sender tag]);
}

Console Output is Every time 0.

How do I set the tag in DetailDisclosure?

jww
  • 97,681
  • 90
  • 411
  • 885
Rajaram
  • 101
  • 2
  • 7
  • Can you amend your code to InformationButton.tag= 10; insteat of InformationButton.tag=i; and try again. I believe i is 0 and this is why your log returns 0. – Greg Feb 07 '14 at 09:37
  • Note name `InformationButton` starting with a lower case. – Larme Feb 07 '14 at 09:39
  • Please don't use tags for this! There are much better built-in ways. See http://stackoverflow.com/questions/9876042/annotation-details-after-detail-disclosure-pressed and http://stackoverflow.com/questions/9462699/how-to-recognize-which-pin-was-tapped. –  Feb 07 '14 at 11:52

1 Answers1

1

As mention above you shouldn't name your variables with upper case letters, this in Objective c usually indicates a Class.

Try using this syntax instead to set the Tag's value.

[InformationButton setTag:i];

Also try NSLog(@"tag to set = %d", i);

So you can see what's being set at the time the code is run.

AppHandwerker
  • 1,758
  • 12
  • 22