0

我有一个UIScrollView(灰色),上面有一个UIView(白色)。在视图中,我在MKMapView里面附上了一个带有别针的。

由于某些未知原因,该引脚在点击时不显示弹出窗口。这里有一些代码:

        UIView * whiteMapView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        whiteMapView.backgroundColor = [UIColor whiteColor];
        whiteMapView.clipsToBounds = NO;
        whiteMapView.userInteractionEnabled = YES;
        [self.scrollView addSubview:whiteMapView];

        x = 10.0f;
        y = x;
        w = CGRectGetWidth(whiteMapView.frame) - (x * 2);
        h = w;

        self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        self.mapView.delegate = self;
        self.mapView.scrollEnabled = NO;
        [whiteMapView addSubview:self.mapView];


        [self.mapView removeAnnotations:self.mapView.annotations];

        Shop * shop = [[Shop alloc] init];
        shop.name = self.userInfo[@"shopName"];
        shop.shopDescription = self.userInfo[@"goods_description"];
        shop.latitude = self.userInfo[@"lat"];
        shop.longitude = self.userInfo[@"lon"];

        MapPin * pin = [[MapPin alloc] initWithShop:shop];
        [self.mapView addAnnotation:pin];

这里的委托方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation
{
    if (annotation == mapView.userLocation)
    {
        return nil;
    }
    else
    {
        MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
        pinView.userInteractionEnabled = YES;
        pinView.image = [UIImage imageNamed:@"pin_mappa_red"];
        pinView.canShowCallout = YES;
        pinView.animatesDrop = NO;

        UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        disclosureButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        disclosureButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [disclosureButton addTarget:self action:@selector(didPressPin:) forControlEvents:UIControlEventTouchUpInside];
        disclosureButton.tag = [[[(MapPin *)annotation shop] idshop] intValue];

        pinView.rightCalloutAccessoryView = disclosureButton;

        return pinView;
    }
}

在应用程序的另一部分,此代码完美运行。

想法?

在此处输入图像描述

4

2 回答 2

0

检查此方法是否调用

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
NSLog("Pin called");
}

如果不调用此方法,则 mapview 或 Annotation 可能存在任何问题。

于 2013-09-19T08:56:15.783 回答
0

尝试viewForAnnotation如下更改

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

    MKPinAnnotationView *pinView = nil; 

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

    if(annotation) 
    {
        UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        UIImageView *imageInMap = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"net.png"]];

        [disclosureButton addTarget:self action:@selector(mapCallOutPressed) forControlEvents:UIControlEventTouchUpInside];


        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[self.locationMapVIew dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                          initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinView.pinColor = MKPinAnnotationColorGreen; 

        pinView.rightCalloutAccessoryView = disclosureButton;
        pinView.leftCalloutAccessoryView = imageInMap;
        pinView.leftCalloutAccessoryView.frame = CGRectMake(0, 0, 35, 35);
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    } 
    else {
        [self.locationMapVIew.userLocation setTitle:@"I am here"];
    }
    return pinView;
}
于 2013-09-19T08:51:24.477 回答