Objective c 刷新注释属性并刷新注释

Objective c 刷新注释属性并刷新注释,objective-c,iphone,annotations,android-mapview,Objective C,Iphone,Annotations,Android Mapview,我有一个位于mapview上的mkannotation,它有一个mkannotationview和一个calloutview,当单击该视图时,它会转到一个子uiviewcontroller。我正在从详图索引的uiviewcontroller更新某些属性,但在完成后,我希望移动注释在地图上的位置并更改注释标题和副标题。如何从详图索引的UIViewController轻松执行此操作?最优雅的处理方式是什么?如果任何人都有代码示例,那么代码示例将非常有用 谢谢我会创建一个协议,比如MapCallBac

我有一个位于mapview上的mkannotation,它有一个mkannotationview和一个calloutview,当单击该视图时,它会转到一个子uiviewcontroller。我正在从详图索引的uiviewcontroller更新某些属性,但在完成后,我希望移动注释在地图上的位置并更改注释标题和副标题。如何从详图索引的UIViewController轻松执行此操作?最优雅的处理方式是什么?如果任何人都有代码示例,那么代码示例将非常有用


谢谢

我会创建一个协议,比如MapCallBackDelegate,来处理您想要做的事情。这避免了紧密耦合的代码。将其放在地图注释视图头文件中

@protocol MapCallBackDelegate
    -(void)updateAnnotation:(id)whatEverParamsYouWant;
@end
然后让你的地图视图实现这个协议。创建地图注释视图时,请为其指定属性

@property (nonatomic, retain) id<MapCallBackDelegate> callbackDelegate;
因此,当您想要更改标题/副标题/位置时,只需在callbkacDelegate上调用该消息


这是很优雅的,因为它减少了紧密耦合的代码,允许其他对象实现相同的协议以便以后重用代码,并促进了MapAnnotationView中的信息隐藏。

最简单的方法是实际上不从子视图控制器执行此操作。也许你的需求与我从问题中理解的不同,但乍一看,我会这样做:

在标题中:

@interface YourController
{
    ...
    MKAnnotation *_latestDetailViewed;
}

... 
@property(nonatomic, retain) MKAnnotation *latestDetailViewed;

@end
然后在第二天,我有点像

@implementation YourController

... 
@synthesize latestDetailViewed = _latestDetailViewed;

...
-(void) dealloc
{
    ...
    self.latestDetailViewed = nil;
    [super dealloc];
}

-(void) whereverYouLaunchYourDetailScreenFrom:(MKAnnotation*)detailAnnotation
{
    self.latestDetailViewed = detailAnnotation;
    // then create/push your view controller
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if(_latestDetailViewed)
    {
        // Do whatever you want to the annotation here
        self.latestDetailViewed = nil;
    }
}
这样,当您返回地图时,您将进行更改。如果一次只启动一个注释的详细视图,并且总是在两者之间返回映射,那么它应该可以工作,而不需要编写委托协议或触发通知


如果我误解了您的情况,请告诉我,我会给您一个不同的答案:)

从地图中完全删除注释,更新它,然后再次将其添加到地图中。这将确保地图注意到注释位置已更改

尽管您可以按照@Caleb的建议删除并添加注释,但另一个选项是直接在要移动的注释上更新
坐标
属性

请注意,只有当注释类实现了
setCoordinate
时,这才有效,这可以通过将
coordinate
声明为
assign
(与内置的
MKPointAnnotation
类类似)而不是
readonly
轻松完成。地图视图将通过KVO查看更改并移动注释


要让子视图控制器告诉地图视图控制器要更改的注释以及新坐标是什么,另一个答案是,我建议使用代理+协议。

@Zap:我没有完全理解你的意思。你能帮我Mahatmanic先生找到距离我当前位置最近的位置吗,比如Gus站和其他热点位置,在2.5klm之间,或者在不同的klm iphone地图应用程序上。如果有任何解决方案,请发送并帮助我,请提供我的id(harishdy@gmail.com)首先,启动地图应用程序。然后,在屏幕顶部有一个搜索栏。在搜索栏中,你应该键入“加油站”。
@implementation YourController

... 
@synthesize latestDetailViewed = _latestDetailViewed;

...
-(void) dealloc
{
    ...
    self.latestDetailViewed = nil;
    [super dealloc];
}

-(void) whereverYouLaunchYourDetailScreenFrom:(MKAnnotation*)detailAnnotation
{
    self.latestDetailViewed = detailAnnotation;
    // then create/push your view controller
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if(_latestDetailViewed)
    {
        // Do whatever you want to the annotation here
        self.latestDetailViewed = nil;
    }
}