活动指示器不旋转
我正在尝试向我的应用程序添加一个旋转活动指示器(UIActivityIndicatorView),同时解析来自互联网的数据。我有一个 IBOutlet(旋转器)连接到 IB 中的 UIActivityIndicatorView。最初我是这样设置的:
-
(void) function {
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
self.spinner.hidesWhenStopped = YES;
[spinner startAnimating];
//parse data from internet
[spinner stopAnimating];}
但是旋转器不旋转。我读到这与所有内容都在同一线程上有关。所以我尝试了这个:
- (void) newFunction {
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
self.spinner.hidesWhenStopped = YES;
[spinner startAnimating];
[NSThread detachNewThreadSelector: @selector(function) toTarget: self withObject: nil];
[spinner stopAnimating];}
但仍然没有运气。有什么想法吗?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
newFunction:
方法应如下所示:并且您的
function
方法应如下所示:Your
newFunction:
method should look like this:And your
function
method should look like this:您不应该再次初始化指示器。请用此替换您的代码。
谢谢。
you should not intitialize indicator again .please replace your code with this.
Thanks.
只需查看“//parse data from internet”是同步还是异步即可。异步意味着一个单独的线程将从该点开始,并且当前函数的执行将立即继续。
在第二个示例中,您显式创建单独的线程,这意味着
@selector(function)
将在单独的线程上发生,下一个语句[spinner stopAnimating]
是立即执行。所以,旋转器似乎根本没有旋转。此外,请确保仅在主线程上启动和停止活动指示器。
Just see that the "//parse data from internet " is synchronous or asynchronous. Asynchronous would mean that a separate thread would start from that point on, and the current function execution will continue without delay.
In your second example, you are explicitly making separate thread, which means that
@selector(function)
will happen on a separate thread, and the next statement[spinner stopAnimating]
is executed immediately. So, it seems like spinner is not spinning at all.Moreover, make sure you start and stop the activity indicator on main thread only.