jdog
jdog

Reputation: 10769

Adding an annotation to mkmap, but want to remove part of the annotation but it won't remove

I create an annotation, which contains several elements textbubble and pin. I turn the bubble on when I show annotation, but later I want to shut the bubble off and leave the annotation.

Here are my two methods. The add subview works, but remove subview does not.

-(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 /////

There seems to be some confusion. This code above is not in the viewController that holds my mkmap, but rather the code inside my custom annotation. Further, I don't want to hide or show the entire annotation based on selecting or deselecting. The self.etaView is custom view which is just part of the annotation. My annotation consists of a custom map pin and an eta bubble. Once the ETA is counted down to 0, I want to remove the bubble (aka self.etaView), but the annotation (map pin) needs to stay on the map the entire time. I just want to hide the ETA bubble.

I am using the proper addAnnotation methods, in the proper way, in my viewController that holds my mkmap. Again, this code above is inside my custom annotation and I want my custom annotation to be responsible for removing its own elements, NOT removing itself from the map.

Upvotes: 0

Views: 430

Answers (2)

jdog
jdog

Reputation: 10769

The method to remove the bubble was getting called, but it just wasn't getting removed? So what I have done is create notification listener on my annotation and post a notification when I want it removed and it removes it. Not sure why it doesn't just work by calling an instance method?

Anyway, notifications solved it. Need to move on so I can launch the app.

Upvotes: 0

Vivienne Fosh
Vivienne Fosh

Reputation: 1778

Come on, why using this weird logics with addSubView and removeFromSuperView. MKMapView is built to support "datasource" for pins. I dunno what kind of view you are trying to acheive but this CGRectMake(-34, -97, 89, 59) looks awful. So please, use method:

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

This way you will have no difficulties managing the annotation using method

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

For example:

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

Upvotes: 2

Related Questions