需要有关活动指示器的帮助

发布于 2024-09-11 07:40:02 字数 1265 浏览 4 评论 0原文

我无法让我的活动指示器工作。

这就是我所得到的-

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
//Create an instance of activity indicator view
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
//set the initial property
[activityIndicator stopAnimating];
[activityIndicator hidesWhenStopped];
//Create an instance of Bar button item with custome view which is of activity indicator
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
//Set the bar button the navigation bar
[self navigationItem].rightBarButtonItem = barButton;
//Memory clean up
[activityIndicator release];
[barButton release];
}

代码中应该启动然后结束的部分--

...
    else if ([theSelection isEqualToString: @"Update statistics"])
    {
        [self startTheAnimation];
        [updateStatistics  updateThe2010Statistics];
        [self stopTheAnimation];
    }
...


-(void)startTheAnimation {
    [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView startAnimating];
}

-(void)stopTheAnimation {
    [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView stopAnimating];
}

I can't get my activity indicator to work.

Here's what I've got-

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
//Create an instance of activity indicator view
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
//set the initial property
[activityIndicator stopAnimating];
[activityIndicator hidesWhenStopped];
//Create an instance of Bar button item with custome view which is of activity indicator
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
//Set the bar button the navigation bar
[self navigationItem].rightBarButtonItem = barButton;
//Memory clean up
[activityIndicator release];
[barButton release];
}

The part of the code that is supposed to get it started and then ended--

...
    else if ([theSelection isEqualToString: @"Update statistics"])
    {
        [self startTheAnimation];
        [updateStatistics  updateThe2010Statistics];
        [self stopTheAnimation];
    }
...


-(void)startTheAnimation {
    [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView startAnimating];
}

-(void)stopTheAnimation {
    [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView stopAnimating];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好听的两个字的网名 2024-09-18 07:40:02

至少更改:

   [activityIndicator hidesWhenStopped];

到:

   activityIndicator.hidesWhenStopped = YES;

或删除该行,因为 YES 是默认值。

At least change:

   [activityIndicator hidesWhenStopped];

To:

   activityIndicator.hidesWhenStopped = YES;

Or remove that line, since YES is default.

好倦 2024-09-18 07:40:02

您很可能会阻塞系统的事件线程:您是否正在执行从某个 IBAction 回调或系统触发的任何其他方法调用 [updateStatistics updateThe2010Statistics]; 的方法(像 -viewDidLoad-viewWillAppear 或类似的)?

在这种情况下,您的长时间运行的任务将阻塞事件线程,从而无法更新您的活动指示器。尝试执行以下操作:

...
else if ([theSelection isEqualToString: @"Update statistics"])
{
  [self startTheAnimation];
  [self performSelectorInBackground:@selector(doUpdateStatistics) withObject:nil];
}
...

- (void) doUpdateStatistics {
  [updateStatistics  updateThe2010Statistics];
  [self performSelectorOnMainThread:@selector(stopTheAnimation) withObject:nil waitUntilDone:NO];
}

这将在第二个线程上执行统计更新,以便事件线程可以正确更新活动指示器。在更新统计信息结束时,我们再次在主线程(即事件线程)上调用停止动画来停止您的活动指示器。

Most likely you're suffering from blocking the system's event thread: are you executing that method where you're calling [updateStatistics updateThe2010Statistics]; from some IBAction callback or any other method that is triggered by the system (like -viewDidLoad, -viewWillAppear or similar)?

In that case your long-running task will block the event thread which in turn cannot update your activity indicator. Try to do the following:

...
else if ([theSelection isEqualToString: @"Update statistics"])
{
  [self startTheAnimation];
  [self performSelectorInBackground:@selector(doUpdateStatistics) withObject:nil];
}
...

- (void) doUpdateStatistics {
  [updateStatistics  updateThe2010Statistics];
  [self performSelectorOnMainThread:@selector(stopTheAnimation) withObject:nil waitUntilDone:NO];
}

This will execute your statistics update on a second thread so that your event thread can properly update the activity indicator. At the end of updating the statistics, we're calling stop animation on the main thread (ie. the event thread) again to stop your activity indicator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文