UIActivityIndi​​cator未在导航栏中显示此代码

时间:2012-10-21 08:02:32

标签: objective-c ios multithreading ios6 uiactivityindicatorview

我尝试使用以下代码在此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] );
         }


      }];
     }
    }

1 个答案:

答案 0 :(得分:2)

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

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

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

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