3

I'd like to stop the animation of the indicator within a method called by default NSNotificationCenter with a postNotificationName. So I'm doing this on Main Thread

-(void)method
{
    ...
    [ind performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
}

It does'n work. Method is called correctly, any other called selectors do their job but not stopAnimating. I put [ind stopAnimating] in another function and then called it via performSelectorOnMainThread but it still didn't worked.

user3290180
  • 4,260
  • 9
  • 42
  • 77

3 Answers3

5

Try this...

Create a method that stops your animation

-(void)stopAnimationForActivityIndicator
{
    [ind stopAnimating];
}

Replace your method like this -

-(void)method
{
    ...
    [self performSelectorOnMainThread:@selector(stopAnimationForActivityIndicator) withObject:nil waitUntilDone:NO];
}

Should do the magic...

Mayur Deshmukh
  • 1,169
  • 10
  • 25
3

You can also use the below method which starts and stops the activity indicator on main thread in a single method, also provides you to execute your code asynchronously as well-

- (void)showIndicatorAndStartWork
{
    // start the activity indicator (you are now on the main queue)
    [activityIndicator startAnimating];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // do your background code here

        dispatch_sync(dispatch_get_main_queue(), ^{
            // stop the activity indicator (you are now on the main queue again)  
        [activityIndicator stopAnimating];
        });
    });
}
Sanjay Mohnani
  • 5,947
  • 30
  • 46
2

Try :

-(void)method
{
    dispatch_async(dispatch_get_main_queue(), ^{ 
       [ind stopAnimating];   
    });
}
itsji10dra
  • 4,603
  • 3
  • 39
  • 59