加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 将数据存储在MKAnnotation中?

发布时间:2020-12-15 01:41:20 所属栏目:百科 来源:网络整理
导读:所以我对Xcode和Objective-C很新.我有几个ArtPiece对象具有存储在数组中的各种属性.然后我将它们添加为MKAnnotations到mapview.我需要做的就是发送一个对点阵数组位置的引用.我相信MKAnnotations只有数据成员的标题,图像和位置,所以当我从对象进行MKAnnotati
所以我对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;

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读