Rya*_*yan 15 arrays xcode objective-c mkmapview mkannotation
所以我对Xcode和Objective-C很陌生.我有几个ArtPiece对象,各种属性存储在一个数组中.然后我将它们作为MKAnnotations添加到mapview.我需要做的是在点击时发送它们来自的阵列位置的引用.我相信MKAnnotations只有数据成员标题,图像和位置,因此当我从对象创建MKAnnotation时,注释不会保留任何其他属性.所以,我的问题是,我如何设法保持对创建注释的对象的数组位置的引用,以便我可以将其发送到其他方法,以便他们可以从数组中检索有关该对象的其他信息以获取详细信息views等有没有办法在注释中存储单个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
Run Code Online (Sandbox Code Playgroud)
这是.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
Run Code Online (Sandbox Code Playgroud)
然后我继续创建该类的对象,设置它们的值,并将它们添加到AppDelegate中的数组中.然后,在另一个类中,我使用:
[self.mapView addAnnotations:mainDelegate.mapAnnotations];
Run Code Online (Sandbox Code Playgroud)
将注释添加到地图.但是,当我尝试在viewforannotation方法中设置"artist"属性时,我得到"请求成员艺术家不是结构或联合".
显然,我必须把这个子类化的东西弄错了,但是我该改变什么呢?
这是我现在拥有的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;
}
Run Code Online (Sandbox Code Playgroud)
所以,我应该:annotation = (ArtPiece *)annotation
简而言之,我的目标是能够在此注释中存储对象的数组位置的引用,以便我可以将其发送给可以访问该对象以获取详细视图等的其他方法.
小智 19
在该viewForAnnotation方法中,您可以访问ArtPiece属性但是为了避免编译器错误和警告,您需要首先将annotation参数强制转换为自定义类.该annotation方法中的参数只是定义为,id<MKAnnotation>因此编译器不会知道特定于ArtPiece的属性(直到你告诉它是一个实例ArtPiece).
你需要这样的东西:
ArtPiece *artPiece = (ArtPiece *)annotation;
NSString *artist = artPiece.artist;
Run Code Online (Sandbox Code Playgroud)
编辑:
按下注释视图的标注按钮时,地图视图将调用calloutAccessoryControlTapped委托方法.此方法MKAnnotationView在view参数中传递.注释对象本身位于参数的annotation属性中view.您可以将该对象强制转换为自定义类以访问您的ArtPiece属性:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
ArtPiece *artPiece = (ArtPiece *)view.annotation;
NSString *artist = artPiece.artist;
}
Run Code Online (Sandbox Code Playgroud)
换句话说,callout按钮处理程序不需要访问原始数组.它获取对实际ArtPiece对象本身的引用.
| 归档时间: |
|
| 查看次数: |
5937 次 |
| 最近记录: |