2013-07-06 72 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

的方法去除泡沫是越來越叫,但它只是沒有被刪除?所以我所做的就是在我的註釋中創建通知監聽器,並在需要刪除它時刪除通知。不確定它爲什麼不通過調用實例方法來工作?

無論如何,通知解決了它。需要繼續前進,以便我可以啓動應用程序。