2012-10-21 30 views
-1

我試圖用以下代碼在此MapView中的NavigationBar上顯示UIActivityIndi​​cator。但是,無論我嘗試哪種方式,我都無法顯示它。這裏有什麼竅門?另外,我正試圖從服務器獲取地圖註釋並在地圖上顯示背景,這是否是這樣做的?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]); 
     } 


     }]; 
    } 
    } 
+0

不知道爲什麼有人低估了這一點。請至少留下評論,以便我瞭解爲什麼。 – banditKing

回答

2

您在顯示它後立即隱藏活動指示器。

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

的問題是,你認爲performSelectorInBackground:withObject:等待方法來完成 - 我真的不明白,爲什麼你認爲它,因爲它隱含其名稱中有它的異步行爲...

儘管如此,將[self.activityIndicatorView stopAnimating];調用移動到回調塊將解決此「問題」。

+0

@ H2C03。謝謝你的幫助。我仍然遇到一些併發編程。這工作。 – banditKing