将数据存储在 MKAnnotation 中? [英] Store data in MKAnnotation?

查看:45
本文介绍了将数据存储在 MKAnnotation 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对 Xcode 和 Objective-C 还是很陌生.我有几个 ArtPiece 对象,它们具有存储在数组中的各种属性.然后我将它们作为 MKAnnotations 添加到地图视图中.我需要能够做的是发送对单击时它们来自的数组位置的引用.我相信 MKAnnotations 只有数据成员标题、图像和位置,所以当我从对象创建 MKAnnotation 时,注释不会保留任何其他属性.所以,我的问题是,我如何设法保持对创建注释的对象的数组位置的引用,以便我可以将其发送给其他方法,以便他们可以从数组中检索有关对象的其他信息以获取详细信息视图等有没有办法在注释中存储单个 int 值?我不认为还有其他想法吗?

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?

这是我的 ArtPiece.h:

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

这里是.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 中的数组中.然后,在另一个类中,我使用:

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];

将注释添加到地图.但是,当我尝试在 viewforannotation 方法中设置艺术家"属性时,我收到在非结构或联合体中对成员艺术家的请求".

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?

这是我现在拥有的 viewforannotation 方法.test.artist... 行不工作.

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;    
}

所以,我应该:annotation = (ArtPiece *)annotation简而言之,我的目标是能够在此注释中存储对对象数组位置的引用,以便我可以将其发送给可以访问该对象以获取详细信息等的其他方法.

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.

推荐答案

viewForAnnotation 方法中,您可以访问您的 ArtPiece 属性,但要避免编译器错误和警告,您需要首先将 annotation 参数转换为您的自定义类.该方法中的 annotation 参数仅定义为 id,因此编译器不会知道特定于 ArtPiece 的属性(直到您告诉它这是一个ArtPiece 的实例).

In the viewForAnnotation method, you can access your ArtPiece properties but to avoid compiler errors and warnings, you'll need to first cast the annotation parameter to your custom class. The annotation parameter in that method is just defined as a id<MKAnnotation> so the compiler won't know about the ArtPiece-specific properties (until you tell it that is an instance of ArtPiece).

你需要这样的东西:

ArtPiece *artPiece = (ArtPiece *)annotation;
NSString *artist = artPiece.artist;


当注释视图的标注按钮被按下时,地图视图调用 calloutAccessoryControlTapped 委托方法.此方法通过 view 参数中的 MKAnnotationView 传递.注释对象本身位于 view 参数的 annotation 属性中.您可以将该对象转换为您的自定义类以访问您的 ArtPiece 属性:


When an annotation view's callout button is pressed, the map view calls the calloutAccessoryControlTapped delegate method. This method gets passed the MKAnnotationView in the view parameter. The annotation object itself is in the annotation property of the view parameter. You can cast that object to your custom class to access your ArtPiece properties:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    ArtPiece *artPiece = (ArtPiece *)view.annotation;
    NSString *artist = artPiece.artist;
}

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

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 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆