Refresh view after UIBarButtonItem is clicked

I have a refresh button on my navigationbar

buttonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(buttonItemClicked)];
    self.navigationItem.rightBarButtonItem = buttonItem;
-(void)buttonItemClicked{
    NSLog(@"buttonItemclicked");
    myView.labelName.text = nil;
    myView.otherLabelName.text = nil;
    [spinner startAnimating]
    [spinnerView setHidden:NO];
    [self requestAPI];
    [spinner stopAnimating];
    [spinnerView setHidden:YES];
  }

If I go in and out of the view, it works fine. But when I call the same methods in buttonItemClicked, it doesn´t work. I also tried calling the view methods inside my action method, but that doesn´t work either.

  • How to bind an Objective-C static library to Xamarin.iOS?
  • Immutable/Mutable Collections in Swift
  • dispatch_async block on main queue is never execeuted
  • how to make objects appear from one side when I rotate them in iOS ? (code + gif ) inside
  • Draw glow around inside edge of multiple CGPaths
  • Sha256 in Objective-C for iPhone
  • What I´m trying to do is set my labels to nil, add my UIActivityIndicatorView and remove it after the labels are set again.

    I have already tried [self.view setNeedsDisplay];

    The refresh it self works, but the animations doesn´t work.

    Any suggestions?

    2 Solutions Collect From Internet About “Refresh view after UIBarButtonItem is clicked”

    The animation is not working because you call startAnimating and stopAnimating (and setHidden) in the same method.
    The render start at the end of the method call.
    You need to set

    [spinner stopAnimating];
    [spinnerView setHidden:YES];
    

    in requestAPI.

    Edit:

    Using Grand Central Dispatch. Like:

    - (void)buttonItemClicked {
      myView.labelName.text = nil;
      myView.otherLabelName.text = nil;
      [spinner startAnimating]
      [spinnerView setHidden:NO];
      [self requestAPI];
    }
    
    - (void)requestAPI {
      dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSURL *url = [NSURL URLWithString:@"http://example.com"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        NSString *stringResult = [[NSString alloc] initWithData:data
                                                       encoding:NSUTF8StringEncoding];
        dispatch_async(dispatch_get_main_queue(), ^{
          [spinner stopAnimating];
          [spinnerView setHidden:YES];
    
          myView.labelName.text = stringResult;
        });
    
      });
    }
    

    Try [myView setsNeedToDisplay];.