-1

我尝试使用以下代码在此 MapView 中的 NavigationBar 上显示 UIActivityIndi​​cator。但是,无论我尝试哪种方式,我都无法展示它。这里有什么诀窍?另外,我正在尝试从服务器获取地图注释并在后台显示在地图上,这是这样做的方法吗?

 - (void)viewDidLoad
 {
  [super viewDidLoad];

  self.activityIndicatorView = [[UIActivityIndicatorView alloc]                            
              initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  self.activityIndicatorView.hidesWhenStopped = YES;
  self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
                                    initWithCustomView:self.activityIndicatorView];

 [self.activityIndicatorView startAnimating];
 [self performSelectorInBackground:@selector(loadMapAnnotations) withObject: nil];
 [self.activityIndicatorView stopAnimating];
 }

 //=========================================================================
 -(void)loadMapAnnotations
 {
   self.mapView.showsUserLocation = YES;
   if(self.vManager = [VendorManager getVendorManager])
   {
     NSLog(@"VendorManager = %@",[self.vManager description] );

     [self.vManager vendorsNearLocation:userLocation block:^(NSArray *vendors, 
      NSError *error)
      {
          if(vendors && [vendors count])
          {
             for (id v in vendors)
             {
                 Vendor *aVendor = [[Vendor alloc] initWithAttributes:v];
                 NSLog(@"Vendor from Vendors = %@",[aVendor name]);
                 [self.mapView addAnnotation:aVendor];
              }
         }
         else
         {
             NSLog(@"Failed to get vendors: %@", [error description] );
         }


      }];
     }
    }
4

1 回答 1

2

您在显示活动指示器后立即隐藏它。

[self.activityIndicatorView startAnimating];
[self performSelectorInBackground:@selector(loadMapAnnotations) withObject: nil];
[self.activityIndicatorView stopAnimating];

问题是你认为performSelectorInBackground:withObject:等待方法完成 - 我真的不明白你为什么认为它这样做,因为它的名字隐含着它的异步行为......

尽管如此,将[self.activityIndicatorView stopAnimating];调用移动到回调块将解决这个“问题”。

于 2012-10-21T08:06:38.387 回答