xcode 将数据存储在 MKAnnotation 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5939223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:00:19  来源:igfitidea点击:

Store data in MKAnnotation?

objective-carraysxcodemkmapviewmkannotation

提问by Ryan

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?

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

Here is my ArtPiece.h:

这是我的 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:

这是.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:

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

[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."

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

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.

这是我现在拥有的 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;    
}

So, should I: annotation = (ArtPiece *)annotationIn 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.

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

回答by

In the viewForAnnotationmethod, you can access your ArtPieceproperties but to avoid compiler errors and warnings, you'll need to first cast the annotationparameter to your custom class. The annotationparameter 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).

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

You'll need something like this:

你需要这样的东西:

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

Edit:
When an annotation view's callout button is pressed, the map view calls the calloutAccessoryControlTappeddelegate method. This method gets passed the MKAnnotationViewin the viewparameter. The annotation object itself is in the annotationproperty of the viewparameter. You can cast that object to your custom class to access your ArtPieceproperties:

编辑:
按下注释视图的标注按钮时,地图视图调用calloutAccessoryControlTapped委托方法。此方法MKAnnotationViewview参数中传递。注释对象本身在参数的annotation属性中view。您可以将该对象强制转换为您的自定义类以访问您的ArtPiece属性:

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

In other words, the callout button handler doesn't need to access the original array. It gets a reference to the actual ArtPieceobject itself.

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

回答by Mark Granoff

Create a subclass of MKAnnotation and you can add whatever additional data members you need.

创建 MKAnnotation 的子类,您可以添加所需的任何其他数据成员。

回答by kball

There seems to be some confusion here.

这里似乎有些混乱。

MKAnnotationis a protocol, not a class, so you don't subclass it, you implement it. Any object can be an MKAnnotationas long as it implements the MKAnnotationprotocol (coordinate, title, subtitle) and that object can have any other property you like if you're creating it. You have done that correctly above.

MKAnnotation是一个协议,而不是一个类,所以你不继承它,你实现它。任何对象都可以是一个MKAnnotation,只要它实现了MKAnnotation协议(坐标、标题、副标题),并且该对象可以具有您喜欢的任何其他属性(如果您正在创建它)。你在上面做对了。

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.

<MKAnnotation>正如 Anna Karenina 所说,您经常需要将其转换为您的自定义类,以通知编译器它是什么类型的对象。

MKAnnotationViewon the other hand is a class (and a subclass of UIView), 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 associated MKAnnotation, so in your custom MKAnnotationViewyou can:

MKAnnotationView另一方面是一个类(和 的子类UIView),如果你想创建你自己的你可以子类化它。然后当它被地图创建和使用时,它持有对其关联的 的引用MKAnnotation,因此在您的自定义中,MKAnnotationView您可以:

self.artistLabel.text = [(ArtPiece *)self.annotation artist];

回答by ajay_nasa

Every annotation has unique id. Use NSMutableDictionaryand use your annotation unique id as key for any objects you want. later you could access when you call this:

每个注释都有唯一的 id。使用NSMutableDictionary并使用您的注释唯一 ID 作为您想要的任何对象的键。稍后您可以在调用时访问:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {