0

我尝试管理注释,并在选择 PIN 时在视图右侧显示一个信息按钮,我的相关代码是这样的:

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

    MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"greenPin"];

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

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

            newAnnotation.pinColor = MKPinAnnotationColorGreen;
            newAnnotation.animatesDrop = YES;
            newAnnotation.canShowCallout = YES;
            newAnnotation.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypeInfoLight];

            return newAnnotation;


        }else {
            newAnnotation.pinColor = MKPinAnnotationColorGreen;
            newAnnotation.animatesDrop = YES;
            newAnnotation.canShowCallout = YES;
            return newAnnotation;
        }

ManageAnnotations.m :

@implementation ManageAnnotations

@synthesize pinColor;
@synthesize storeName=_storeName;
@synthesize storeAdress=_storeAdress;
@synthesize coordinate=_coordinate;


-(id)initWithTitle:(NSString*)storeName adress:(NSString*)storeAdress coordinate:(CLLocationCoordinate2D)coordinate{

    if((self=[super init])){
        _storeName=[storeName copy];
        _storeAdress=[storeAdress copy];
        _coordinate=coordinate;

    }
    return self;

}
-(NSString*)title{

    return _storeName;
}
-(NSString*)subtitle{

    return _storeAdress;


}

ManageAnnotations.h

@interface ManageAnnotations : NSObject<MKAnnotation>{

    NSString *_storeName;
    NSString *_storeAdress;
    CLLocationCoordinate2D _coordinate;

}
//
@property(nonatomic,assign)MKPinAnnotationColor pinColor;
@property(nonatomic, readonly, copy)NSString *storeName;
@property(nonatomic, readonly, copy)NSString *storeAdress;
@property(nonatomic,readonly)CLLocationCoordinate2D coordinate;



//
-(id)initWithTitle:(NSString*)storeName adress:(NSString*)storeAdress coordinate:(CLLocationCoordinate2D)coordinate;
//

PIN 码在地图上正确显示,但视图右侧没有信息按钮。我错过了什么吗?

4

1 回答 1

1
  1. 你真的进入这种状态了吗?设置断点进行检查。
  2. 为什么一开始就创建一个 MKPinAnnotationView,在你知道注解的类型之前?
  3. 你应该出队你的 annotationView 而不是 alloc/initWithAnnotation:reuseIdentifier
  4. 当你重用你的注解时,你应该把所有不会改变的东西(颜色、动画等)放在 alloc init 之后,而不是一直重置它们。否则你会失去重用的兴趣。

除此之外,您的代码看起来不错,并且将其与我的代码进行比较,我看不到任何明显的东西。备注 1 是最可能的。我会设置一个断点,看看我是否真的去那里,看看我是否可以显示一个leftCalloutAccessoryView,使用不同的pinColor。

于 2012-06-17T15:14:18.293 回答