活动指示器不旋转 [英] Activity Indicator doesn't spin
本文介绍了活动指示器不旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试添加一个旋转活动指示器(UIActivityIndicatorView)到我的应用程序,当它解析来自互联网的数据。我有一个IBOutlet(微调)连接到IB中的UIActivityIndicatorView。最初,我设置为这样:
I'm trying to add a spinning activity indicator (UIActivityIndicatorView) to my app while it parses data from the internet. I have an IBOutlet (spinner) connected to a UIActivityIndicatorView in IB. Initially I had it set up like this:
-
(void) function {
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
self.spinner.hidesWhenStopped = YES;
[spinner startAnimating];
//parse data from internet
[spinner stopAnimating];}
旋转器不会旋转。我读到它与一切都在同一个线程有关。所以我尝试这个:
But the spinner wouldn't spin. I read that it had something to do with everything being on the same thread. So I tried this:
- (void) newFunction {
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
self.spinner.hidesWhenStopped = YES;
[spinner startAnimating];
[NSThread detachNewThreadSelector: @selector(function) toTarget: self withObject: nil];
[spinner stopAnimating];}
但是没有运气。有任何想法吗?感谢。
But still no luck. Any ideas? Thanks.
推荐答案
您的 newFunction:
方法应如下所示: / p>
Your newFunction:
method should look like this:
- (void) newFunction {
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
self.spinner.hidesWhenStopped = YES;
[NSThread detachNewThreadSelector: @selector(function) toTarget: self withObject: nil];
}
您的函数
方法应该是这样:
- (void) function {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self.spinner performSelectorOnMainThread:@selector(startAnimating) withObject:nil waitUntilDone:NO];
//...
[self.spinner performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
[pool drain];
}
这篇关于活动指示器不旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文