Refresh view after UIBarButtonItem is clicked

2019-03-03 02:01发布

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.

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条回答
一夜七次
2楼-- · 2019-03-03 02:20

Try [myView setsNeedToDisplay];.

查看更多
爷、活的狠高调
3楼-- · 2019-03-03 02:30

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

  });
}
查看更多
登录 后发表回答