2

所以我创建了一个 X 类,如下所示:

@interface X : NSObject <MKAnnotation> {
    CLLocationCoordinate2D  coordinate;
    NSString * title;
    NSString * subtitle;
    UIImage * image;
    NSInteger * tag;
}

@property (nonatomic, readonly) CLLocationCoordinate2D  coordinate;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * subtitle;
@property (nonatomic, retain) UIImage * image;
@property (nonatomic, readwrite) NSInteger * tag;

@end

在 - 的里面:

  • (void) mapView: (MKMapView *) mapView annotationView:(MKAnnotationView *) view calloutAccessoryControlTapped:(UIControl *) 控件

我希望能够访问 X 拥有的标签属性。这怎么可能?我可以做[控制标签]吗?这假设如何工作?

4

1 回答 1

1

对于第二部分,警告的原因是您将纯整数分配给 NSInteger 指针。NSInteger是一种类型intlong

所以你正在做(错误地):

NSInteger * tag = 2;

编辑:

这就是你可以使用 NSInteger 的方式:

NSInteger myi = 42;
NSLog(@"int: %d", myi);

NSInteger * i = &myi;    // i is a pointer to integer here
*i = 43;                 // dereference the pointer to change 
                         // the value at that address in memory
NSLog(@"int: %d", myi);

鉴于上述情况,您正在尝试:

NSInteger * i = &myi;
i = 2;                  // INCORRECT: i is an pointer to integer

声明tagNSInteger而不是在属性中NSInteger*使用assign(我会给你确切的代码,但我在 linux atm ...)。

编辑结束

对于第一部分,我不确定如何将对象传递给您的方法,但是如果该方法是该方法用于从 object 获取数据的接口的一部分,X那么您应该能够做到这一点。[yourobject tag]tagX

我的意思是MKAnnotation协议没有tag属性,因此您必须将对象类型转换为您的对象类型,例如,或者对象来自X *anX = (X*)self.annotation;何处,那么您应该能够访问标签, - 如果那是您的对象annotation[anX tag]X

我在使用自定义注释的 Apple 文档中找到了这个示例代码

在示例中,注释设置在视图中。

绘制视图时,它使用来自实现注释协议的对象的数据。在访问值之前将对象类型转换为实际对象(参见视图的绘制方法)。

您可以在控制器中看到如何regionDidChangeAnimated在视图中设置新注释。

于 2011-02-01T23:49:22.883 回答