Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 用于更改leftCalloutAnnotationView的自定义MKAnnotation类_Iphone_Objective C - Fatal编程技术网

Iphone 用于更改leftCalloutAnnotationView的自定义MKAnnotation类

Iphone 用于更改leftCalloutAnnotationView的自定义MKAnnotation类,iphone,objective-c,Iphone,Objective C,我已经为MKAnnotation创建了一个自定义类 @interface NeighborMapAnnotation : NSObject <MKAnnotation> { CLLocationCoordinate2D coordinate; NSString * title; NSString * subtitle; UIImageView * lcav; } @property (nonatomic, readonly) CLLocationC

我已经为MKAnnotation创建了一个自定义类

@interface NeighborMapAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D  coordinate;
    NSString * title;
    NSString * subtitle;
    UIImageView * lcav;
}

@property (nonatomic, readonly) CLLocationCoordinate2D  coordinate;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * subtitle;
@property (nonatomic, retain) UIImageView * lcav;
@end



NeighborMapAnnotation *neighbor = [[NeighborMapAnnotation alloc] initWithCoordinate:aCoordinate];
//imgView is some UIImageView that I already have
neighbor.leftCalloutAccessoryView = imgView;


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

    MKPinAnnotationView * annView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"custom view"] autorelease];
    annView.animatesDrop = YES;
    annView.canShowCallout = YES;
    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annView.rightCalloutAccessoryView = rightButton;
    annView.leftCalloutAccessoryView = [annotation lcav];//is this right?

    return annView;
}
@interface-neightrmapannotation:NSObject{
CLLocationCoordinate2D坐标;
NSString*标题;
NSString*副标题;
UIImageView*lcav;
}
@属性(非原子,只读)CLLocationCoordinate2D坐标;
@属性(非原子,保留)NSString*标题;
@属性(非原子,保留)NSString*副标题;
@属性(非原子,保留)UIImageView*lcav;
@结束
NeighborMacanNotation*neighbor=[[NeighborMacanNotation alloc]initWithCoordinate:acoordination];
//imgView是我已经拥有的一些UIImageView
neighbor.leftCalloutAccessoryView=imgView;
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
MKPinAnnotationView*annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“自定义视图”]autorelease];
annView.animatesDrop=是;
annView.canShowCallout=是;
UIButton*rightButton=[UIButton Button类型:UIButtonTypedTailButton];
annView.rightCalloutAccessoryView=rightButton;
annView.leftCalloutAccessoryView=[annotation lcav];//是否正确?
返回视图;
}

我希望能够将注释的leftCalloutAccessoryView更改为图像。我应该在自定义的MKAnnotation类中更改哪些代码?

leftCalloutAccessoryView属性是MKAnnotationView类的属性(不在MKAnnotation协议中)

在MKMapView的viewForAnnotation委托方法中,为注释提供MKAnnotationView。在该方法中,使用isKindOfClass检查注释类型是否为自定义注释类型,并根据需要设置视图的属性

在NeightarPanNotation类中,可以将imageView存储为UIImageView属性,并在创建注释时进行设置

在viewForAnnotation中,可以将视图的leftCalloutAccessoryView设置为注释的imageView

编辑:
这里有一个实现它的方法

将图像属性添加到自定义注释类:

@interface NeighborMapAnnotation : NSObject <MKAnnotation> {
    ...
    UIImage *image;
}
...  
@property (nonatomic, retain) UIImage *image;
@end
在viewForAnnotation委托方法中(确保设置了地图视图的委托):

-(MKAnnotationView*)映射视图:(MKMapView*)注释的映射视图:(id)注释
{
if([annotation isKindOfClass:[NeightrmaPanNotation class]])
{
静态NSString*AnnotationIdentifier=@“AnnotationIdentifier”;
MKPinAnnotationView*pinView=(MKPinAnnotationView*)[mapView出列可重用AnnotationViewWithIdentifier:AnnotationIdentifier];
如果(!pinView)
{
pinView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:AnnotationIdentifier]autorelease];
pinView.canShowCallout=是;
pinView.animatesDrop=是;
}
其他的
{
pinView.annotation=注释;
}
UIImageView*leftCalloutView=[[UIImageView alloc]
initWithImage:((NeightarPanNotation*)注释.image];
pinView.leftCalloutAccessoryView=leftCalloutView;
[左视图发布];
返回pinView;
}
返回零;
}

太好了!我试图通过几个属性扩展我的自定义注释类。但我无法访问viewForAnnotation委托的内部。我在这里错过并学到的是,我必须在自定义注释类上强制转换附加属性。现在它工作了!谢谢!:-)

这是否允许我对不同管脚的不同图像使用不同的LeftCallout AccessoryView?是的,我将很快添加一些示例代码。MapCallouts示例应用程序也展示了如何做到这一点。我刚刚看到了这个示例,但这意味着每个pin的图像都是相同的,对吗?我希望它能够有不同的形象。。。我想说neighbor.lcav=some UIImageView我根据我的理解编辑了上面的代码,请让我知道我做错了什么您的代码现在看起来应该可以工作了,除了
neighbor.leftCalloutAccessoryView=imgView应该是
neighbor.lcav=imgView。您还将在
[annotation lcav]
行上收到警告。请按照下面的建议编辑上面的代码
NeighborMapAnnotation *neighbor = [[NeighborMapAnnotation alloc] initWithCoordinate:aCoordinate];
neighbor.image = [UIImage imageNamed:@"someimage.png"];
//the image could be different for each annotation
...
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[NeighborMapAnnotation class]])
    {
        static NSString *AnnotationIdentifier = @"AnnotationIdentifier";
        MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
        if (!pinView)
        {
            pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
            pinView.canShowCallout = YES;
            pinView.animatesDrop = YES;
        }
        else
        {
            pinView.annotation = annotation;
        }

        UIImageView *leftCalloutView = [[UIImageView alloc] 
            initWithImage:((NeighborMapAnnotation *)annotation).image];
        pinView.leftCalloutAccessoryView = leftCalloutView;
        [leftCalloutView release];

        return pinView;
    }

    return nil;
}