objective-c – 将数据存储在MKAnnotation中?

objective-c – 将数据存储在MKAnnotation中?,第1张

概述所以我对Xcode和Objective-C很新.我有几个ArtPiece对象具有存储在数组中的各种属性.然后我将它们添加为MKAnnotations到mapview.我需要做的就是发送一个对点阵数组位置的引用.我相信MKAnnotations只有数据成员的标题,图像和位置,所以当我从对象进行MKAnnotation时,注释不会保留任何其他属性.所以,我的问题是,我如何设法保留对创建注释的对象的数组 所以我对Xcode和Objective-C很新.我有几个ArtPIEce对象具有存储在数组中的各种属性.然后我将它们添加为MKAnnotations到mapvIEw.我需要做的就是发送一个对点阵数组位置的引用.我相信MKAnnotations只有数据成员的标题,图像和位置,所以当我从对象进行MKAnnotation时,注释不会保留任何其他属性.所以,我的问题是,我如何设法保留对创建注释的对象的数组位置的引用,以便我可以将其发送到其他方法,以便他们可以从数组中检索关于对象的附加信息,以获取详细信息视图等.有没有办法在注释中存储单个int值?我不认为有什么其他的想法?

这是我的ArtPIEce.h:

#import <Foundation/Foundation.h>#import <MapKit/MapKit.h>@interface ArtPIEce : NSObject <MKAnnotation>{    Nsstring *Title;    Nsstring *artist;    Nsstring *serIEs;    Nsstring *description;    Nsstring *location;    Nsstring *area;    NSNumber *latitude;    NSNumber *longitude;    UIImage *image;}@property (nonatomic,retain) Nsstring *Title;@property (nonatomic,retain) Nsstring *artist;@property (nonatomic,retain) Nsstring *serIEs;@property (nonatomic,retain) Nsstring *description;@property (nonatomic,retain) Nsstring *location;@property (nonatomic,retain) Nsstring *area;@property (nonatomic,retain) NSNumber *latitude;@property (nonatomic,retain) NSNumber *longitude;@property (nonatomic,retain) UIImage *image;@end

这里是.m:

#import "ArtPIEce.h"#import "FindArtController.h"#import "ArtPIEce.h"#import <MapKit/MapKit.h>@implementation ArtPIEce- (Nsstring *)description{    return [Nsstring stringWithFormat:@"Title: %@",Title];}@synthesize Title,artist,serIEs,description,latitude,longitude,location,area,image;- (CLLocationCoordinate2D)coordinate{    CLLocationCoordinate2D theCoordinate;    theCoordinate.latitude = [self.latitude doubleValue];    theCoordinate.longitude = [self.longitude doubleValue];    return theCoordinate;}@end

然后,我继续创建该类的对象,设置它们的值,并将它们添加到AppDelegate中的数组中.然后,在另一个班上,我使用:

[self.mapVIEw addAnnotations:mainDelegate.mapAnnotations];

将注释添加到地图.但是,当我尝试在vIEwofannotation方法中设置“艺术家”属性时,我得到“要求成员不是结构或联盟的东西”.

显然,我一定是做这个子类的事情错了,但是我该怎么改?

这里是我现在的意见方法. test.artist …行不行.

- (MKAnnotationVIEw *)mapVIEw:(MKMapVIEw *)mapVIEw vIEwForAnnotation:(ID <MKAnnotation>)annotation{    MKAnnotationVIEw *test=[[MKAnnotationVIEw alloc] initWithAnnotation:annotation reuseIDentifIEr:@"reuse"];    test.image = [UIImage imagenamed:@"pao_pin.png"];    [test setCanShowCallout:YES];    [test setUserInteractionEnabled:YES];    test.rightCalloutAccessoryVIEw =    [UIbutton buttonWithType:UIbuttonTypeDetaildisclosure];    test.artist = annotation.artist;    return test;    }

所以,我应该:注释=(ArtPIEce *)注释
简而言之,我的目标是能够存储对这个注释中对象的数组位置的引用,这样我就可以发送给可以访问该对象的其他方法来获取详细视图.

解决方法 在VIEwForAnnotation方法中,您可以访问ArtPIEce属性,但为避免编译错误和警告,您需要先将注释参数转换为自定义类.该方法中的注释参数被定义为ID< MKAnnotation>所以编译器不知道ArtPIEce特定的属性(直到你告诉它是ArtPIEce的一个实例).

你需要这样的东西:

ArtPIEce *artPIEce = (ArtPIEce *)annotation;Nsstring *artist = artPIEce.artist;

编辑:
当按下注释视图的标注按钮时,地图视图将调用calloutAccessoryControlTapped委托方法.该方法通过视图参数中的MKAnnotationVIEw.注释对象本身在vIEw参数的注释属性中.您可以将该对象转换为您的自定义类来访问您的ArtPIEce属性:

- (voID)mapVIEw:(MKMapVIEw *)mapVIEw annotationVIEw:(MKAnnotationVIEw *)vIEw calloutAccessoryControlTapped:(UIControl *)control{    ArtPIEce *artPIEce = (ArtPIEce *)vIEw.annotation;    Nsstring *artist = artPIEce.artist;}

换句话说,标注按钮处理程序不需要访问原始数组.它引用了实际的ArtPIEce对象本身.

总结

以上是内存溢出为你收集整理的objective-c – 将数据存储在MKAnnotation中?全部内容,希望文章能够帮你解决objective-c – 将数据存储在MKAnnotation中?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1065522.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存