CommentsView 委托和 MKAnnotation

发布于 2024-10-15 08:41:53 字数 711 浏览 3 评论 0原文

因此,我创建了一个类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-10-22 08:41:53

对于第二部分,警告的原因是您将纯整数分配给 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 对象

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

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

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

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

For the second part, the reason for the warning is that you assign plain integer to NSInteger pointer. NSInteger is of a type int or long

So you are doing (incorrectly):

NSInteger * tag = 2;

EDIT:

This is how you could use 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);

Given above, you are trying:

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

Declare tag as NSInteger instead of NSInteger* and use assign 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 the tag method is part of the interface that the method is using to get data from object X.

What I mean is that the MKAnnotation protocol does not have tag property so you have to typecast the object to your type of object e.g. X *anX = (X*)self.annotation; ,or wherever the annotation object comes from, then you should be able to access tag, [anX tag] - if that is the your X object

I'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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文