简体   繁体   中英

iOS: how to properly stop an Activity Indicator?

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.

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...

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];
        });
    });
}

Try :

-(void)method
{
    dispatch_async(dispatch_get_main_queue(), ^{ 
       [ind stopAnimating];   
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM