1

由于某种原因,我无法立即显示活动指示器。也许任何人都可以看到为什么?

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = self.view.center; 
_active = spinner;
[self.view addSubview:spinner];
[spinner startAnimating];
@try {

    [self connectToServerUsingStream:ip portNo:port];
    NSString *text = @"test";
    const uint8_t *str =
    (uint8_t *) [text cStringUsingEncoding:NSASCIIStringEncoding];
    [self writeToServer:str];

}
@catch (NSException * e) {
    NSLog(@"Exception: %@", e);
}

微调器只显示然后连接到服务器,在此之前它不显示。有任何想法吗?

4

3 回答 3

3

使用下面更新的代码。我认为它会起作用。

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = self.view.center; 
 _active = spinner; 
[self.view addSubview:spinner]; 
[spinner startAnimating]; 
[self performSelector:@selector(performTask) withObject:nil afterDelay:0.0];

- (void)performTask {
     @try {
         [self connectToServerUsingStream:ip portNo:port];
         NSString *text = @"test";
         const uint8_t *str =
         (uint8_t *) [text cStringUsingEncoding:NSASCIIStringEncoding];
         [self writeToServer:str];

     }
     @catch (NSException * e) {
         NSLog(@"Exception: %@", e);
     } 
}
于 2012-11-30T06:53:00.657 回答
0

替换下面的代码

[微调器 performSelectorOnMainThread:@selector(startAnimating) withObject:nil waitUntilDone:TRUE];

代替..

[微调开始动画];
于 2012-11-30T06:53:48.423 回答
0

方法后的这一行[spinner startAnimating];阻塞了主线程,这就是活动未显示的原因。

请更改该代码,如:

dispatch_queue_t taskQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(taskQ, ^{
   @try
   {

     [self connectToServerUsingStream:ip portNo:port];
     NSString *text = @"test";
     const uint8_t *str =
     (uint8_t *) [text cStringUsingEncoding:NSASCIIStringEncoding];
     [self writeToServer:str];
   }
   @catch (NSException * e)
   {
     NSLog(@"Exception: %@", e);
   }
});
于 2012-11-30T06:56:12.173 回答