1

I have two info buttons showing up within my callout view for some reason. I have 3 different pin colors that are pulling information from 3 different sources. I need each button for each pin color to push to a separate view controller based on that annotation class. i have the following code so far for my 3 different pin colors:

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

    if([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }

    static NSString *identifier = @"MyLocation";
    if ([annotation isKindOfClass:[annotation class]]) {

        MKPinAnnotationView *view = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (view == nil) {
            view = [[MKPinAnnotationView alloc]
                    initWithAnnotation:annotation
                    reuseIdentifier:identifier];
        } else {

            view.annotation = annotation;
        }

       if([[(Annotation*)annotation phoneNumber] isEqualToString:@"0"]){


            view.enabled = YES;
            view.canShowCallout = YES;
            view.pinColor = MKPinAnnotationColorPurple;
            // Create a UIButton object to add on the
            self.leftBtn = [UIButton buttonWithType:UIButtonTypeInfoDark];
            [self.leftBtn setTitle:annotation.title forState:UIControlStateNormal];

            [view setLeftCalloutAccessoryView:self.leftBtn];

        }else{

            view.enabled = YES;
            view.canShowCallout = YES;

            // Create a UIButton object to add on the
            self.rightBtn = [UIButton buttonWithType:UIButtonTypeInfoDark];
            [self.rightBtn setTitle:annotation.title forState:UIControlStateNormal];

            [view setRightCalloutAccessoryView:self.rightBtn];

        }

        if ([[(Annotation*)annotation phoneNumber] isEqualToString:@"1"]){

            view.enabled = YES;
            view.canShowCallout = YES;
            view.pinColor = MKPinAnnotationColorGreen;
            // Create a UIButton object to add on the
            self.leftBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [self.leftBtn setTitle:annotation.title forState:UIControlStateNormal];

            [view setLeftCalloutAccessoryView:self.leftBtn];

        }


        return view;
    }

    return nil;
}

And for my accessory tapped method I have the following:

- (void)mapView:(MKMapView *)map annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{

    if(view.leftCalloutAccessoryView){


        UserAnnotation *annotate = view.annotation;
        BuydealsViewController *dealsView=[[BuydealsViewController alloc]initWithNibName:@"DealDetailsViewViewController" bundle:[NSBundle mainBundle]];

        dealsView.urlString = annotate.url;
        [self.navigationController pushViewController:dealsView animated:YES];
}
    if(view.rightCalloutAccessoryView){

        MapDealViewController *mapDeals = [[MapDealViewController alloc] init];
        Annotation *annView = view.annotation;
        mapDeals.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        mapDeals.phoneNumber = annView.phoneNumber;
        mapDeals.address = annView.subtitle;
        mapDeals.title = annView.title;
        mapDeals.description = annView.description;
        mapDeals.value = annView.value1;
        mapDeals.discountPercent = annView.discountPercent;
        mapDeals.price = annView.price;
        mapDeals.header = annView.header;
        mapDeals.imageUrl = annView.imageUrl;
        mapDeals.url = annView.url;
        mapDeals.dealy = annView.dealy;

        self.navigationController.navigationBarHidden = NO;
        [self.navigationController pushViewController:mapDeals animated:YES];

}



}

Is there any way to make them both right buttons but click to separate views?

4

1 回答 1

1

首先,这目前可能不会引起任何问题,但必须指出。
这一行在viewForAnnotation

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

看起来不对,因为当然会是和 它本身annotation一样的类。 这将永远返回。
YES

你可能打算Annotation class用一个大写A的类名来写(annotation小写a是指当前的注释实例参数):

if ([annotation isKindOfClass:[Annotation class]])


关于“出于某种原因,我的标注视图中显示了两个信息按钮。”

查看当前逻辑的简化版本viewForAnnotation

if (phoneNumber is "0") 
{
    ...
    [view setLeftCalloutAccessoryView:self.leftBtn];
}
else
{
    ....
    [view setRightCalloutAccessoryView:self.rightBtn];
}

if (phoneNumber is "1") 
{
    ....
    [view setLeftCalloutAccessoryView:self.leftBtn];
}

请注意,如果 phoneNumber 为“1”:

  • 视图的rightCalloutAccessoryView设置是else因为“1”不是“0”,
  • 并且视图leftCalloutAccessoryView设置在第二个中if(因为它是“1”)。

设置附件视图的逻辑需要根据您的需要进行调整。

请注意,当给定的注释不应该具有该附件(以及其他注释可能时)时,您应该显式设置视图的或leftCalloutAccessoryView。这是因为如果一个注解视图被重用,它的附属视图可能已经基于它之前使用的注解设置了。所以你可能会做这样的事情:rightCalloutAccessoryViewnil

if (phoneNumber is "0")
{
    ...
    view.leftCalloutAccessoryView = nil;
    view.rightCalloutAccessoryView = [UIButton buttonWithType...];
}
else if (phoneNumber is "1")
{
    ...
    view.leftCalloutAccessoryView = [UIButton buttonWithType...];
    view.rightCalloutAccessoryView = nil;
}
else
{
    //some default, unexpected case handling...
    ...
    view.leftCalloutAccessoryView = nil;
    view.rightCalloutAccessoryView = nil;
}

此外,没有必要拥有leftBtnrightBtn作为属性,可能会导致混淆或其他问题。只需将它们声明为局部变量。


关于“有没有办法让它们都成为右按钮但单击以分隔视图?”

是的。您已经有viewForAnnotation执行此操作的代码:

  • 您可以检查annotation对象的class(但使用名而不是实例变量正确执行)。
  • 您可以annotation转换为您的自定义类并检查自定义属性值(如phoneNumber)。
于 2013-11-08T02:54:01.460 回答