Question

i want hide and start the activity indicator every time the user tap on top of the mkannotationPinView.

this is my code:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    if (!self.activityIndicator) {
        NSLog(@"error");
    }
    self.activityIndicator.hidden = NO;
    [self.mapView addSubview:self.activityIndicator];
    [self.activityIndicator startAnimating];
    if ([view.leftCalloutAccessoryView isKindOfClass:[UIImageView class]]) {
        UIImageView *imageView = (UIImageView *)(view.leftCalloutAccessoryView);
        if ([view.annotation respondsToSelector:@selector(thumbnail)]) {
            imageView.image = [view.annotation performSelector:@selector(thumbnail)];
        }
    }
    [self.activityIndicator stopAnimating];
}

i have try also to switch [self.mapView addSubview:self.activityIndicator];with [self.view addSubview:self.activityIndicator];but the effect is the same, notting appare on the view. whats the problem??? thanks

Was it helpful?

Solution

Queue code after StartAnimating on Main thread again. You can use

  • dispath_asyn
  • PerformSelectorOnMainThread
  • PerformSelectorInBackground

All three should work just fine.

    -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
    {
        if (!self.activityIndicator) {
            NSLog(@"error");
        }
        self.activityIndicator.hidden = NO;
        [self.mapView addSubview:self.activityIndicator];
        [self.activityIndicator startAnimating];

        dispatch_async(dispatch_get_main_queue(), ^(void) {
           if ([view.leftCalloutAccessoryView isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView = (UIImageView *)(view.leftCalloutAccessoryView);
                if ([view.annotation respondsToSelector:@selector(thumbnail)]) {
                    imageView.image = [view.annotation performSelector:@selector(thumbnail)];
                }
                [self.activityIndicator stopAnimating];
          }
        });
    }

Explanation: If you do to UI operation without queuing up the operations in main thread effect wont be visible because UI wont show any activity until and unless you finish running the thread and move to next operating in main thread. Same is valid for addSubview removeSubview too.

OTHER TIPS

I don't know if this can solve your problem but I think you're not implementing the activity indicator properly. The activity indicator is used to be shown while things are being downloaded/updated/whatever in background. So.. Your code has to be something like this:

-(void)methodThatShowsTheActivityIndicator{
    self.activityIndicator.hidden = NO;
    [self.activityIndicator startAnimating];
    [self performSelectorInBackground:@selector(backgroundProcess) withObject:self];
}

-(void)backgroundProcess{
    //some process
    [self performSelectorOnMainThread:@selector(finishLoad) withObject:self waitUntilDone:false];
}


-(void)finishLoad{
    self.activityIndicator.hidden = YES;
    [self.activityIndicator stopAnimating];
}

Hope it helped!

  1. Be sure that you are allocated your activity indicator
  2. If it so - you can't see it because hidesWhenStopped property of activity indicator by default is set to YES. Your code will be executed very fast and you wont even see activity indicator
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top