Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.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 我的自定义MKAnnotationView不是';不可点击_Iphone_Ios_Objective C_Mkannotationview - Fatal编程技术网

Iphone 我的自定义MKAnnotationView不是';不可点击

Iphone 我的自定义MKAnnotationView不是';不可点击,iphone,ios,objective-c,mkannotationview,Iphone,Ios,Objective C,Mkannotationview,我正在开发一个自定义注释视图,该视图显示背景图像,其中包含一个自定义图像。 我的代码基于以下内容: 我的代码是: else if ([annotation isKindOfClass:[ImagePin class]]){ static NSString *identifierImage = @"ImagePinLocation"; MKAnnotationView *annotationImageView = (MKAnnotationView *) [s

我正在开发一个自定义注释视图,该视图显示背景图像,其中包含一个自定义图像。 我的代码基于以下内容: 我的代码是:

    else if ([annotation isKindOfClass:[ImagePin class]]){
       static NSString *identifierImage = @"ImagePinLocation";
        MKAnnotationView *annotationImageView = (MKAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifierImage];
       if(annotationImageView){
         return annotationImageView;
       }
       else {
            annotationImageView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifierImage];
            annotationImageView.canShowCallout = YES;

            annotationImageView.image = [UIImage imageNamed:@"image_bg_mappa"];

            UIImage *frame = [UIImage imageNamed:@"image_bg_mappa"];
            ImagePin *imagePinImage = annotation;
            NSLog(@"%@", [NSString stringWithFormat:@"%@", imagePinImage.image]);


            NSString *urlStringForImage = [NSString stringWithFormat:@"%@", imagePinImage.image];
            NSURL *urlForImage = [NSURL URLWithString:urlStringForImage];
            UIImageView *imageView = [[UIImageView alloc] init];

            [imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", urlForImage]] placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]];

            UIGraphicsBeginImageContext(CGSizeMake(annotationImageView.image.size.width, annotationImageView.image.size.height));

            [frame drawInRect:CGRectMake(0, 0, frame.size.width, frame.size.height)];
            [imageView.image drawInRect:CGRectMake(2, 2, 48, 38)]; // the frame your inner image

            annotationImageView.image = UIGraphicsGetImageFromCurrentImageContext();

            UIGraphicsEndImageContext();

            UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside];
            [rightButton setTitle:@"" forState:UIControlStateNormal];
            annotationImageView.canShowCallout = YES;
            annotationImageView.rightCalloutAccessoryView = rightButton;
            annotationImageView.enabled = YES;
        return annotationImageView;
        }
    }
然后我有两种方法:

(void)writeSomething {        
}

(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {    
}
但是:

  • 不显示右侧按钮标注(不同时显示按钮)
  • 我更喜欢在单击完整图像/注释时调用mapView annotationView方法
  • 我怎么能做到?为什么当我单击annotation时,它不调用我的calloutAccessoryControlTapped方法? 在这张地图中,我还有更多不同类型的插针(mkpinannotation),并且工作正常(calloutAccessoryControlTapped方法也适用于这些插针)。 问题在哪里? 谢谢大家,对不起我的英语不好(我正在学习)

    编辑:我的viewForAnnotation方法的完整代码: MKPinAnnotation工作正常

    编辑: 这是我的个人密码

    #import <Foundation/Foundation.h>
    #import <MapKit/MapKit.h>
    @interface ImagePin : NSObject <MKAnnotation>
    
    - (id)initWithImage:(NSString*)image imagebeachId:(NSString*)imagebeachId coordinate:(CLLocationCoordinate2D)coordinate;
    //- (MKMapItem*)mapItem;
    @property (nonatomic, copy) NSString *imagebeachId;
    @property (nonatomic, copy) NSString *image;
    @property (nonatomic, assign) CLLocationCoordinate2D theCoordinate;
    @end
    
    #导入
    #进口
    @接口ImagePin:NSObject
    -(id)initWithImage:(NSString*)图像Beachid:(NSString*)图像Beachid坐标:(CLLocationCoordinate2D)坐标;
    //-(MKMapItem*)mapItem;
    @属性(非原子,副本)NSString*imagebeachId;
    @属性(非原子,复制)NSString*图像;
    @属性(非原子,赋值)ClLocationCoordinated2和Coordinate;
    @结束
    
    还有我的ImagePin.m

    #import "ImagePin.h"
    #import <AddressBook/AddressBook.h>
    
    @interface ImagePin ()
    
    @end
    
    @implementation ImagePin
    @synthesize image = _image;
    @synthesize imagebeachId = _imagebeachId;
    @synthesize theCoordinate = _theCoordinate;
    
    - (id)initWithImage:(NSString*)image imagebeachId:(NSString*)imagebeachId coordinate:(CLLocationCoordinate2D)coordinate {
        if ((self = [super init])) {
            //self.address = address;
            self.image = image;
            self.imagebeachId = imagebeachId;
            self.theCoordinate = coordinate;
        }
        return self;
    }
    - (NSString *)title {
        return @"";
    }
    - (NSString *)subtitle {
        return _image;
    }
    
    - (CLLocationCoordinate2D)coordinate {
        return _theCoordinate;
    }
    @end
    
    #导入“ImagePin.h”
    #进口
    @接口ImagePin()
    @结束
    @实现ImagePin
    @合成图像=_图像;
    @合成imagebeachId=\u imagebeachId;
    @综合坐标=_坐标;
    -(id)initWithImage:(NSString*)图像Beachid:(NSString*)图像Beachid坐标:(CLLocationCoordinate2D)坐标{
    if((self=[super init])){
    //self.address=地址;
    自我形象=形象;
    self.imagebeachId=imagebeachId;
    self.theCoordinate=坐标;
    }
    回归自我;
    }
    -(NSString*)标题{
    返回@”;
    }
    -(NSString*)副标题{
    返回图像;
    }
    -(CLLocationCoordinate2D)坐标{
    返回协调;
    }
    @结束
    
    编辑: 这是我添加注释的地方:

    - (void)imagePositions:(NSDictionary *)responseData {
        for (id<MKAnnotation> annotation in self.mapView.annotations) {
            if ([annotation isKindOfClass:[ImagePin class]]){
                //[self.mapView removeAnnotation:annotation];
            }
        }
    
        for (NSArray *row in responseData) {
            NSString * imageBeachId = [row valueForKey:@"id"];
    
            NSNumber * latitude = [row valueForKey:@"lat"];
    
    
            NSNumber * longitude = [row valueForKey:@"lng"];
            NSString * imageBeach = [row valueForKey:@"fb_photo_url"];
    
    
            CLLocationCoordinate2D coordinate;
            coordinate.latitude = latitude.doubleValue;
            coordinate.longitude = longitude.doubleValue;
            ImagePin *annotation = [[ImagePin alloc] initWithImage:imageBeach imagebeachId:imageBeachId coordinate:coordinate];
    
            [self.mapView addAnnotation:annotation];
        }
    }
    
    -(无效)图像位置:(NSDictionary*)响应数据{
    for(self.mapView.annotations中的id注释){
    if([annotation isKindOfClass:[ImagePin class]]){
    //[self.mapView removeAnnotation:annotation];
    }
    }
    用于(响应数据中的NSArray*行){
    NSString*imageBeachId=[行值forkey:@“id”];
    NSNumber*纬度=[row valueForKey:@“lat”];
    NSNumber*经度=[行值forkey:@“lng”];
    NSString*imageBeach=[row valueForKey:@“fb_照片_url”];
    CLLocationCoordinate2D坐标;
    坐标.纬度=纬度.双值;
    coordinate.longitude=longitude.doubleValue;
    ImagePin*注释=[[ImagePin alloc]initWithImage:imageBeach imagebeachId:imagebeachId坐标:坐标];
    [self.mapView addAnnotation:annotation];
    }
    }
    
    首先检查是否将
    委托人
    交给MapView,如果没有,则将
    委托人
    交给以下人员

    yourMapView.delegate = self;
    
    @interface ViewController : UIViewController<MKMapViewDelegate,MKAnnotation>{...
    
    同样在
    .h
    文件中定义
    委托
    ,如下所示

    yourMapView.delegate = self;
    
    @interface ViewController : UIViewController<MKMapViewDelegate,MKAnnotation>{...
    
    更新:

    尝试使用自定义按钮,而不是自定义按钮

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    
        NSLog(@"viewForAnnotation...");
    
        static NSString *identifier = @"MyLocation";
        if ([annotation isKindOfClass:[ImagePin class]]) {
    
            MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    
            if (annotationView == nil)
            {
                annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
            }
    
            if (annotation == mapView.userLocation)
                return nil;
    
            annotationView.image = [UIImage imageNamed:@"yourImageName.png"];
            annotationView.annotation = annotation;
            annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            annotationView.rightCalloutAccessoryView.tag = 101;
    
            annotationView.canShowCallout = YES;
            //  annotationView.animatesDrop = YES;
            return annotationView;
        }
    
        return nil;
    }
    
    -(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
    NSLog(@“viewForAnnotation…”);
    静态NSString*标识符=@“MyLocation”;
    if([annotation isKindOfClass:[ImagePin class]]){
    MKAnnotationView*annotationView=(MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    如果(注释视图==nil)
    {
    annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation重用标识符:标识符]自动释放];
    }
    if(注释==mapView.userLocation)
    返回零;
    annotationView.image=[UIImage ImageName:@“yourImageName.png”];
    annotationView.annotation=注释;
    annotationView.rightCalloutAccessoryView=[UIButton Button,类型:UIButtonTypedTailDisclosure];
    annotationView.rightCalloutAccessoryView.tag=101;
    annotationView.canShowCallout=是;
    //annotationView.animatesDrop=YES;
    返回注释视图;
    }
    返回零;
    }
    
    我已经修复了所有问题! 问题是标题不能为空(我使用了@“”…带有@“AAA”可以正常工作),然后为不显示我所做的标注:

    annotationView.canShowCallout = NO;
    
    并实施:

    -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
    {
        //here you action
    }
    

    非常感谢Anna(解决方案是你的)!

    对于其他在实现自定义MKAnnotationView时偶然发现这篇文章的人,问题可能还在于你必须明确设置视图的框架


    设置
    userInteractionEnabled=YES;
    ?我尝试了annotationImageView.userInteractionEnabled=YES;但不起作用…当我单击时,什么都没有发生。这是我查看注释的完整代码:您在ImagePin中返回一个空白的
    标题
    。如果注释的标题为空,它将不会显示标注(即使canShowCallout是肯定的)。@anna karenina你说得对!有一种方法可以不显示callout,只调用action callout AccessoryControlTapped?我不想在自定义图像pin上显示callout。代理很好,因为我有其他类型的pin(mkpinannotation),工作正常,同时调用calloutAccessoryControlTapped。@RiccardoRossi i update now answer使用
    -(void)WriteMething:(id)sender
    ,而不是您定义的。.((void)WriteMething:(id)sender也不会发生任何情况(且右侧按钮未显示)@RiccardoRossi请查看此链接,我将查看您的全部代码并发表评论,但在此链接中看到另一个类似于您的要求的演示:)我希望它对您有所帮助。。。。