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

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

问题描述

所以我pretty新X code和Objective-C。我有存储在数组中的各种属性几个ArtPiece对象。我然后添加到MKAnnotations到的MapView。我需要能够做的就是发送到他们从点击传来阵列位置的参考。我相信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;    
}

所以,我应该:注释=(ArtPiece *)注释
总之,我的目标是能够存储在此注释对象的数组位置的参考从这么有人认为我可以发送给谁可以访问该对象详细的意见等。

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 但要避免编译器错误和警告,你需要的属性先投了注释参数自定义类。该方法中的注释参数只是定义为 ID&LT; MKAnnotation&GT; 因此,编译器将不知道有关具体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;

编辑:结果
当注释视图的标注按钮pssed $ P $,地图视图调用 calloutAccessoryControlTapped 委托方法。这种方法得到了视图参数传递的 MKAnnotationView 。注释对象本身是在注释视图属性参数。你可以施放该对象到您的自定义类来访问你的 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天全站免登陆