需要有关活动指示器的帮助
我无法让我的活动指示器工作。
这就是我所得到的-
-(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至少更改:
到:
或删除该行,因为 YES 是默认值。
At least change:
To:
Or remove that line, since YES is default.
您很可能会阻塞系统的事件线程:您是否正在执行从某个 IBAction 回调或系统触发的任何其他方法调用
[updateStatistics updateThe2010Statistics];
的方法(像-viewDidLoad
、-viewWillAppear
或类似的)?在这种情况下,您的长时间运行的任务将阻塞事件线程,从而无法更新您的活动指示器。尝试执行以下操作:
这将在第二个线程上执行统计更新,以便事件线程可以正确更新活动指示器。在更新统计信息结束时,我们再次在主线程(即事件线程)上调用停止动画来停止您的活动指示器。
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:
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.