单击UIBarButtonItem后刷新视图 [英] Refresh view after UIBarButtonItem is clicked

查看:55
本文介绍了单击UIBarButtonItem后刷新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导航栏上有一个刷新按钮

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

如果我进出视图,它可以正常工作.但是,当我在buttonItemClicked中调用相同的方法时,它不起作用.我也尝试过在动作方法中调用视图方法,但这也不起作用.

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.

我要做的是将标签设置为nil,添加我的 UIActivityIndi​​catorView ,然后在再次设置标签后将其删除.

What I´m trying to do is set my labels to nil, add my UIActivityIndicatorView and remove it after the labels are set again.

我已经尝试过[self.view setNeedsDisplay];

I have already tried [self.view setNeedsDisplay];

刷新本身是有效的,但是动画不起作用.

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

有什么建议吗?

推荐答案

该动画无法正常工作,因为您使用相同的方法调用startAnimating和stopAnimating(以及setHidden).渲染从方法调用的末尾开始.您需要设置

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

requestAPI 中.

使用Grand Central Dispatch.喜欢:

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

  });
}

这篇关于单击UIBarButtonItem后刷新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆