2011-02-01 37 views
2

所以我必須創建一個類X如下:annotationView代表和MKAnnotation

@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 

裏面的:

  • (無效)的MapView:(的MKMapView *)的MapView annotationView:(MKAnnotationView *)視圖calloutAccessoryControlTapped :(UIControl *)控件

我希望能夠訪問X所擁有的標籤屬性。這怎麼可能? 我可以做[控制標籤]嗎?這是如何工作的?

回答

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 

申報tag作爲NSInteger代替NSInteger*和使用性能assign(我願意給你確切的代碼,但我在linux上ATM ...)。

編輯

對於我不知道是怎麼的X對象傳遞給你的方法第一部分結束了,但你應該就能夠做到[yourobject tag]如果tag方法的一部分該方法用於從對象X獲取數據的接口。

我的意思是MKAnnotation協議不具有tag屬性,因此您必須將對象類型轉換爲您的對象類型,例如, X *anX = (X*)self.annotation;,或其它地方的annotation對象來自,那麼你應該能夠訪問標籤,[anX tag] - 如果這是你的X對象

我發現這個example code in Apple docs that uses custom annotation

在示例中,註釋是在視圖中設置的。

當繪製視圖時,它使用實現註釋協議的對象中的數據。在訪問值之前,該對象被稱爲實際對象(請參見視圖的繪製方法)。

您可以在控制器中看到在視圖中regionDidChangeAnimated中新註釋的設置。

+0

我不明白我是如何做不正確的,我只是初始化對象X然後做了一個X.tag = 2; – aherlambang 2011-02-02 02:01:11