2010-07-23 49 views
0

我无法让我的活动指标正常工作。需要活动指标帮助

这里就是我的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]; 
} 

是应该得到的代码就开始的部分,然后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]; 
} 
+0

它是否显示,但不是动画或甚至没有出现? – progrmr 2010-07-23 17:29:33

回答

0

至少变化:

[activityIndicator hidesWhenStopped]; 

收件人:

activityIndicator.hidesWhenStopped = YES; 

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

0

最有可能你阻塞系统的事件线程痛苦:你执行该方法,你是从一个由系统引起了一些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]; 
} 

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