objective-c – 将数据存储在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; 编辑: - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { ArtPiece *artPiece = (ArtPiece *)view.annotation; NSString *artist = artPiece.artist; } 换句话说,标注按钮处理程序不需要访问原始数组.它引用了实际的ArtPiece对象本身. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |