活动指标不会旋转

ken*_*187 2 cocoa-touch objective-c uikit uiactivityindicatorview

我正在尝试向我的应用添加旋转活动指示器(UIActivityIndi​​catorView),同时解析来自互联网的数据.我有一个IBOutlet(微调器)连接到IB中的UIActivityIndi​​catorView.最初我设置如下:

-

 (void) function {
        self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
 self.spinner.hidesWhenStopped = YES;
 [spinner startAnimating];
 //parse data from internet
 [spinner stopAnimating];}
Run Code Online (Sandbox Code Playgroud)

但旋转器不会旋转.我读到它与同一个线程上的所有内容有关.所以我尝试了这个:

    - (void) newFunction {
        self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
 self.spinner.hidesWhenStopped = YES;
 [spinner startAnimating];
 [NSThread detachNewThreadSelector: @selector(function) toTarget: self withObject: nil];
 [spinner stopAnimating];}
Run Code Online (Sandbox Code Playgroud)

但仍然没有运气.有任何想法吗?谢谢.

Jac*_*kin 8

您的newFunction:方法应如下所示:

- (void) newFunction {
   self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
   self.spinner.hidesWhenStopped = YES;
   [NSThread detachNewThreadSelector: @selector(function) toTarget: self withObject: nil];
}
Run Code Online (Sandbox Code Playgroud)

你的function方法应该是这样的:

- (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];
}
Run Code Online (Sandbox Code Playgroud)