2013-07-06 74 views
0

我创建了一个注释,其中包含多个元素textbubble和pin。我在显示注释时打开了泡泡,但后来我想关闭泡泡并离开注释。向mkmap添加注释,但要删除部分注释但不会删除

这是我的两种方法。添加子视图工作,但删除子视图不。

-(void)hideETACountdown { 
self.etaView.hidden = YES; 
[self.etaView removeFromSuperview]; 
} 

-(void)showETACountdown { 

self.etaView = [[UIView alloc] initWithFrame:CGRectMake(-34, -97, 89, 59)]; 
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"WaitBubble_backgroundandshadow.png"]]; 
[self.etaView addSubview:bg]; 
UILabel *minLabel = [[UILabel alloc] initWithFrame:CGRectMake(7, 24, 42, 21)]; 
minLabel.text = @"min"; 
minLabel.textAlignment = UITextAlignmentCenter; 
minLabel.font = [UIFont systemFontOfSize:10]; 

self.etaLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 4, 30, 27)]; 
self.etaLabel.font = [UIFont boldSystemFontOfSize:22]; 
self.etaLabel.textAlignment = UITextAlignmentCenter; 
self.etaLabel.text = @""; 

[self.etaView addSubview:minLabel]; 
[self.etaView addSubview:self.etaLabel]; 

[self addSubview:self.etaView]; 

self.etaView.hidden = NO; 
} 

- (id) initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier 
{ 
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) { 


    self.canShowCallout = YES; 
    self.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    self.innerImage = [[UIImageView alloc] initWithImage:nil]; 
    self.innerImage.frame = CGRectMake(-15, -38, 32, 39); 

    [self addSubview:self.innerImage]; 

    if(self.showETA) { 

     [NSNotificationCenter addUniqueObserver:self 
             selector:@selector(handleEtaTimeUpdate:) 
              name:kEtaUpdate 
             object:nil]; 
     [self showETACountdown]; 

    } 

} 
return self; 
} 

// UPDATE /////

似乎有一些混乱。上面的代码不在viewController中,它保存我的mkmap,而是在我的自定义注释中的代码。此外,我不想根据选择或取消选择来隐藏或显示整个注释。 self.etaView是自定义视图,它只是注释的一部分。我的注释由一个自定义地图引脚和一个eta气泡组成。一旦ETA计数到0,我想删除气泡(又名self.etaView),但注释(地图别针)需要一直保留在地图上。我只想隐藏ETA泡沫。

我使用正确的方法,在我的viewController持有我的mkmap正确的addAnnotation方法。同样,上面的代码在我的自定义注释中,我希望我的自定义注释负责移除它自己的元素,而不是从地图中移除它自己。

回答

2

加油,为什么使用这个奇怪的逻辑与addSubView和removeFromSuperView。 MKMapView的构建是为了支持针脚的“数据源”。我不知道你想要达到什么样的观点,但这个CGRectMake(-34, -97, 89, 59)看起来很糟糕。所以,请使用方法:

-(MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation 

这样,你不会有任何困难,使用管理的标注方法

- (void)deselectAnnotation:(id <MKAnnotation>)annotation animated:(BOOL)animated 

例如:

[mapView deselectAnnotation:[mapView.selectedAnnotations objectAtIndex:0] animated:YES]; 
+0

是的,不要直接在这里弄乱视图层次结构...使用提供的框架方法 –

+0

添加/删除注释调用addAnotation/removeAnnotation –

+0

解决家伙。上面的代码不在管理地图的我的控制器中。这是我注释中的代码。因此, - (id)initWithAnnotation:(id )注释reuseIdentifier:(NSString *)reuseIdentifier我在我的viewcontroller运行该类使用viewForAnnotation方法。另外请不要假定deselectAnnotation是当我想删除注释的子视图。我从来不想删除一次显示的注释。我也隐藏和显示self.etaView通过代码不屏幕触摸。而self.etaView并不是整个注释只是它的一个视图元素。 – jdog

0

的方法去除泡沫是越来越叫,但它只是没有被删除?所以我所做的就是在我的注释中创建通知监听器,并在需要删除它时删除通知。不确定它为什么不通过调用实例方法来工作?

无论如何,通知解决了它。需要继续前进,以便我可以启动应用程序。