CommentsView 委托和 MKAnnotation
因此,我创建了一个类 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 commentView:(MKAnnotationView *) view calloutAccessoryControlTapped:(UIControl *) 控件
内部我希望能够访问 X 的标记属性有。这怎么可能? 我可以做[控制标签]吗?这应该如何运作?
So I have create a class X as follows:
@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
Inside the:
- (void) mapView: (MKMapView *) mapView annotationView:(MKAnnotationView *) view calloutAccessoryControlTapped:(UIControl *) control
I would like to be able to access the tag property that X has. How is this possible?
Can I do [control tag]? How is this suppose to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第二部分,警告的原因是您将纯整数分配给 NSInteger 指针。
NSInteger
的类型为int
或long
所以你正在做(不正确):
编辑:
这就是你的方式可以使用 NSInteger:
上面给出,您正在尝试:
将
tag
声明为NSInteger
而不是NSInteger*
并使用assign
在财产中(我会给你确切的代码,但我在linux atm上......)。编辑结束
对于第一部分,我不确定
X
的对象如何传递给您的方法,但您应该能够执行[ yourobject tag]
如果tag
方法是该方法用于从对象X
获取数据的接口的一部分。我的意思是
MKAnnotation
协议没有tag
属性,因此您必须将对象类型转换为您的对象类型,例如X *anX = (X* )self.annotation;
,或者无论annotation
对象来自何处,那么您应该能够访问标签[anX tag]
- 如果这是你的 X 对象我找到了这个 Apple 文档中使用自定义注释的示例代码。
在示例中,注释是在视图上设置的。
当绘制视图时,它使用来自实现注释协议的对象的数据。在访问值之前,该对象被类型转换为实际对象(请参阅视图的绘制方法)。
您可以在控制器中看到如何在视图的
regionDidChangeAnimated
中设置新注释。For the second part, the reason for the warning is that you assign plain integer to NSInteger pointer.
NSInteger
is of a typeint
orlong
So you are doing (incorrectly):
EDIT:
This is how you could use NSInteger:
Given above, you are trying:
Declare
tag
asNSInteger
instead ofNSInteger*
and useassign
in property (I'd give you exact code but I'm on linux atm ...).END OF EDIT
For the first part I'm not sure how is the object of
X
being passed to your method, but you should be then able to do[yourobject tag]
if thetag
method is part of the interface that the method is using to get data from objectX
.What I mean is that the
MKAnnotation
protocol does not havetag
property so you have to typecast the object to your type of object e.g.X *anX = (X*)self.annotation;
,or wherever theannotation
object comes from, then you should be able to access tag,[anX tag]
- if that is the yourX
objectI've found this example code in Apple docs that uses custom annotation.
In the example the annotation is set on view.
When view is drawn it uses data from the object that implements annotation protocol. The object is typecasted to the actual object before values are accessed (see drawing method of the view).
You can see in the controller how new annotations are set in
regionDidChangeAnimated
on the view.