更新MKMapView上自定义注释的标题/副标题 [英] Update title/subtitle for custom annotation on an MKMapView

查看:94
本文介绍了更新MKMapView上自定义注释的标题/副标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义注释来显示我的数据。数据来自移动车辆,我必须更新他们在地图上的位置以及他们的标题(它显示最后更新时间)。

I created a custom annotation to display my data. The data comes from moving vehicles and I've got to update their location on the map as well as their title (it shows the last update time).

我的第一个方法是简单地删除所有注释并重新创建它们。然而,这会导致很多可怕的闪烁(地图清除,然后所有注释再次出现)和地图会在重绘时阻塞。不是最佳解决方案。

My first approach was to simply remove all annotations and recreate them. However this leads to a lot of horrible flickering (the map clears, and then all annotations appear again) and the map blocks while it redraws. Less than optimal solution.

在OS4 SDK中,他们改进了 MKMapViewAnnotation ,我现在可以设置坐标(它们之前是只读的)。更改坐标可以很好地移动视图,而不会出现任何闪烁。这解决了我的一个问题。

In the OS4 SDK they improved the MKMapViewAnnotation and I can now set the coordinates (they were readonly before). Changing the coordinates moves the view nicely where it should be without any flickering. That solved one of my issues.

它在API文档中说更改 MKAnnotation 的标题将更新 MKAnnotationView 。确实是 MKPinView 的情况。如果我更新 MKAnnotation 的坐标和标题,则引脚会移动并显示新数据。

It said in the API documentation that changing the title of the MKAnnotation will update the MKAnnotationView. Indeed that's the case for the MKPinView. If I update the MKAnnotation's coordinates and title, the pin moves and shows the new data.

怎么能我使用自定义 MKAnnotationView 获得了相同的结果?我看不出任何方式要求视图刷新。
我试图在 MKAnnotationView 上调用 setNeedsLayout ,但这只会导致视图消失。

How can I achieve the same result using my custom MKAnnotationView? I can't see any way to ask the view to refresh. I tried calling setNeedsLayout on the MKAnnotationView but that only resulted in the view disappearing.

干杯。

推荐答案

MKMapView 使用 KVO 观察对标题字幕属性。确保地图视图注意到您的更改的最简单方法是使它们成为属性并使用 setTitle: / setSubtitle:方法(或 self.title = @...; - 请注意 self。表示法是必需的):

MKMapView uses KVO to observe changes to the title and subtitle properties. The easiest way to ensure the map view notices your changes, is to make them properties and use the setTitle: / setSubtitle: methods (or self.title = @"..."; — note that the self. notation is required):

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

- (void)changeAnnotation;



CustomAnnotation.m



CustomAnnotation.m

@synthesize title, subtitle;

- (void)changeAnnotation {
    self.title = @"New title!";
    [self setSubtitle:@"New subtitle!"];
}

合成属性设置器为您处理键值观察的严格要求。否则,您必须致电 willChangeValueForKey: didChangeValueForKey: 你自己;这些是在 NSObject 中实现的:

The synthesized property setters handle the rigors of key-value observing for you. Otherwise, you'll have to call willChangeValueForKey: and didChangeValueForKey: yourself; these are implemented in NSObject:

[self willChangeValueForKey:@"title"];
[title release];
title = @"New title.";
[self didChangeValueForKey:@"title"];

这篇关于更新MKMapView上自定义注释的标题/副标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆