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

2 回答 2

0

很可能您正遭受阻塞系统事件线程的困扰:您是否正在执行[updateStatistics updateThe2010Statistics];从某个 IBAction 回调或由系统触发的任何其他方法(如-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];
}

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

于 2010-07-23T17:45:43.090 回答
0

至少改变:

   [activityIndicator hidesWhenStopped];

至:

   activityIndicator.hidesWhenStopped = YES;

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

于 2010-07-23T17:30:20.923 回答