-1

下午好,我在使用“活动指示器”时遇到了困难,我希望它出现在我的视图中,同时我将 XML 下载到 NSData 中,下载完成后它变得不可见。

试过了,但只有在下载完成后才会出现指示符。

我使用的代码很简单,启动“Activity indicator”调用服务器URL,转入NSData,然后停止“Activity indicator”,调用另一个View,在WebView中呈现信息,也就是当“Activity indicator”开始加载,我希望在加载 WebView 出现之前显示显示。

4

2 回答 2

1

您需要在不同的线程(不是主线程)上下载。最好的方法是使用 GCD。这是示例代码:

//Start Activity indicator on the main thread, 
[activityIndicator performSelectorOnMainThread:@selector(startAnimating) withObject:nil waitUntilDone:YES];


dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Start Download code

    dispatch_async( dispatch_get_main_queue(), ^{
        [activityIndicator stopAnimating];
    });
});
于 2013-06-20T21:05:35.167 回答
0

这是 UIActivityIndi​​catorView 的示例。它在开始下载之前启动微调器,并在调用块的完成处理程序时停止微调器。请记住仅从主队列更新 UI - 在这种情况下停止微调器。

-(void)test
{
  CGRect screenRect = [[UIScreen mainScreen] bounds];
  CGFloat screenWidth = screenRect.size.width;
  CGFloat screenHeight = screenRect.size.height;

  UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  [activityIndicator setCenter:CGPointMake(screenWidth/2.0, screenHeight/2.0)];
  [self.view addSubview:activityIndicator];
  [activityIndicator startAnimating];

  NSMutableString *myURL = [[NSMutableString alloc] initWithString:@"http://www.domain.com/"];

  NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[myURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];
  NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  [NSURLConnection
   sendAsynchronousRequest:urlRequest
   queue:queue
   completionHandler: ^( NSURLResponse *response,
                      NSData *data,
                      NSError *error)
   {
     if (error == nil)
     {
         // do whatever with data

         dispatch_async(dispatch_get_main_queue(), ^{
             [activityIndicator stopAnimating]; // stop spinner from the main queue
             [activityIndicator removeFromSuperview];
         });
     } else // got an error
     {
         dispatch_async(dispatch_get_main_queue(), ^{
             [activityIndicator stopAnimating]; // stop spinner from the main queue
             [activityIndicator removeFromSuperview];
         });
     }
 }];
}
于 2013-06-21T01:24:57.203 回答