将数据存储在 MKAnnotation 中?
所以我对 Xcode 和 Objective-C 还很陌生。我有几个 ArtPiece 对象,它们具有存储在数组中的各种属性。然后我将它们作为 MKAnnotations 添加到地图视图中。我需要做的是发送对它们单击时来自的数组位置的引用。我相信 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];
将注释添加到地图中。但是,当我尝试在 viewforannotation 方法中设置“艺术家”属性时,我收到“在非结构或联合中请求成员艺术家”。
显然,我一定做错了这个子类化的事情,但我该改变什么?
这是我现在拥有的 viewforannotation 方法。 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;
}
那么,我应该: annotation = (ArtPiece *)annotation
简而言之,我的目标是能够存储对此注释中对象的数组位置的引用,以便我可以将其发送到可以访问该对象以获取详细视图等的其他方法。
So I'm pretty new to Xcode and Objective-C. I have several ArtPiece objects with various attributes stored in an array. I then add them as MKAnnotations to the mapview. What I need to be able to do is send a reference to the array position they came from on click. I believe MKAnnotations only have the data members title, image, and location, so when I make an MKAnnotation from the objects, the annotation does not keep any of the other attributes. So, my question is, how can I manage to keep a reference to the array position of the object the annotation was created from so that I can send it to other methods so they can retrieve the additional information about the object from the array for detail views etc. Is there any way to store a single int value in an annotation? I don't think there is so any other ideas?
Here is my 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
and here is the .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
I then go on to make objects of that class, set their values, and add them to an array in the AppDelegate. Then, in another class, I use:
[self.mapView addAnnotations:mainDelegate.mapAnnotations];
to add the annotations to the map. However, when I try to set the "artist" attribute in the viewforannotation method, I get "request for member artist in something not a structure or union."
Obviously, I must be doing this subclassing thing wrong, but what do I change?
Here is the viewforannotation method I have now. The test.artist... line is not working.
- (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;
}
So, should I: annotation = (ArtPiece *)annotation
In short, my goal is to be able to store a reference to the array position of the object in this annotation was made from so that I can send that to other methods who can access that object for detail views etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在
viewForAnnotation
方法中,您可以访问ArtPiece
属性,但为了避免编译器错误和警告,您需要首先转换annotation
参数到您的自定义课程。该方法中的annotation
参数仅定义为id
,因此编译器不会知道 ArtPiece 特定的属性(除非您告诉它这是一个ArtPiece
的实例)。你需要这样的东西:
编辑:
当按下注释视图的标注按钮时,地图视图将调用
calloutAccessoryControlTapped
委托方法。此方法通过view
参数中的MKAnnotationView
进行传递。注释对象本身位于view
参数的annotation
属性中。您可以将该对象转换为自定义类以访问ArtPiece
属性:换句话说,标注按钮处理程序不需要访问原始数组。它获取对实际
ArtPiece
对象本身的引用。In the
viewForAnnotation
method, you can access yourArtPiece
properties but to avoid compiler errors and warnings, you'll need to first cast theannotation
parameter to your custom class. Theannotation
parameter in that method is just defined as aid<MKAnnotation>
so the compiler won't know about the ArtPiece-specific properties (until you tell it that is an instance ofArtPiece
).You'll need something like this:
Edit:
When an annotation view's callout button is pressed, the map view calls the
calloutAccessoryControlTapped
delegate method. This method gets passed theMKAnnotationView
in theview
parameter. The annotation object itself is in theannotation
property of theview
parameter. You can cast that object to your custom class to access yourArtPiece
properties:In other words, the callout button handler doesn't need to access the original array. It gets a reference to the actual
ArtPiece
object itself.创建 MKAnnotation 的子类,您可以添加所需的任何其他数据成员。
Create a subclass of MKAnnotation and you can add whatever additional data members you need.
这里似乎有些混乱。
MKAnnotation
是一个协议,而不是一个类,因此您不需要子类化它,而是实现它。任何对象都可以是MKAnnotation
,只要它实现MKAnnotation
协议(坐标、标题、副标题),并且该对象可以具有您喜欢的任何其他属性(如果您正在创建)它。上面你已经做对了。通常,您需要将
转换为您的自定义类,正如 Anna Karenina 所说,以告知编译器它是什么类型的对象。另一方面,
MKAnnotationView
是一个类(也是UIView
的子类),如果您想创建自己的类,则可以对其进行子类化。然后,当地图创建并使用它时,它会保存对其关联的MKAnnotation
的引用,因此在自定义MKAnnotationView
中,您可以:There seems to be some confusion here.
MKAnnotation
is a protocol, not a class, so you don't subclass it, you implement it. Any object can be anMKAnnotation
as long as it implements theMKAnnotation
protocol (coordinate, title, subtitle) and that object can have any other property you like if you're creating it. You have done that correctly above.Oftentimes you will need to cast that
<MKAnnotation>
to your custom class, as Anna Karenina said, to inform the compiler what type of object it is.MKAnnotationView
on the other hand is a class (and a subclass ofUIView
), and if you want to create your own you can subclass it. Then when it is created and used by the map it holds a reference to its associatedMKAnnotation
, so in your customMKAnnotationView
you can:每个注释都有唯一的 id。使用 NSMutableDictionary 并使用您的注释唯一 id 作为您想要的任何对象的键。稍后您可以在调用时访问:
Every annotation has unique id. Use
NSMutableDictionary
and use your annotation unique id as key for any objects you want. later you could access when you call this: