2012-08-13 48 views
0

我一直在琢磨MKMapView相當長一段時間,試圖更熟悉它並且遇到了問題。MKPinAnnotationView一次加載didSelectAnnotationView方法中的所有數據

我有我的MapView填充兩個引腳,當我按他們,他們有他們各自的註釋。我也有一個按鈕,將帶我到一個新的UIView UlLabels將加載數組中的數據。

看着我的控制檯,我注意到數據正在通過,而不是它只是針對我選擇其加載所有的引腳,然後顯示最後一個。

這裏是我的我使用加載數據的下一個視圖的方法代碼:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ 

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:addAnnotation  reuseIdentifier:@"currentloc"]; 
    if(annView.tag = 0) { 
      GSStore * theStore = [globalCMS getStoreById:1]; 
      NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults]; 
      int storeid = theStore.storeid; 
      [prefs setInteger:storeid forKey:@"selectedstore"]; 

      NSLog(@"%@", theStore.name); 

      storeName.text = [NSString stringWithFormat:@"%@", theStore.name]; 
      storeCS.text = [NSString stringWithFormat:@"%@,%@", theStore.city, theStore.state]; 
      storeHours.text = [NSString stringWithFormat:@"%@", theStore.hours]; 
      storePhone.text = [NSString stringWithFormat:@"%@", theStore.phone]; 
      storeAddress.text = [NSString stringWithFormat:@"%@", theStore.address]; 
      storeCSZ.text = [NSString stringWithFormat:@"%@,%@ %@", theStore.city, theStore.state, theStore.zip]; 
      storeWebsite.text = [NSString stringWithFormat:@"%@", theStore.website]; 
      } 
    if(annView.tag = 1){ 
      GSStore * theStore = [globalCMS getStoreById:2]; 
      NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults]; 
      int storeid = theStore.storeid; 
      [prefs setInteger:storeid forKey:@"selectedstore"]; 

      NSLog(@"%@", theStore.name); 

      storeName.text = [NSString stringWithFormat:@"%@", theStore.name]; 
      storeCS.text = [NSString stringWithFormat:@"%@,%@", theStore.city, theStore.state]; 
      storeHours.text = [NSString stringWithFormat:@"%@", theStore.hours]; 
      storePhone.text = [NSString stringWithFormat:@"%@", theStore.phone]; 
      storeAddress.text = [NSString stringWithFormat:@"%@", theStore.address]; 
      storeCSZ.text = [NSString stringWithFormat:@"%@,%@ %@", theStore.city, theStore.state, theStore.zip]; 
      storeWebsite.text = [NSString stringWithFormat:@"%@", theStore.website]; 
      } 

    if (annView.tag = 2) { 
      GSStore * theStore = [globalCMS getStoreById:3]; 
      NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults]; 
      int storeid = theStore.storeid; 
      [prefs setInteger:storeid forKey:@"selectedstore"]; 

      NSLog(@"%@", theStore.name); 

      storeName.text = [NSString stringWithFormat:@"%@", theStore.name]; 
      storeCS.text = [NSString stringWithFormat:@"%@,%@", theStore.city, theStore.state]; 
      storeHours.text = [NSString stringWithFormat:@"%@", theStore.hours]; 
      storePhone.text = [NSString stringWithFormat:@"%@", theStore.phone]; 
      storeAddress.text = [NSString stringWithFormat:@"%@", theStore.address]; 
      storeCSZ.text = [NSString stringWithFormat:@"%@,%@ %@", theStore.city, theStore.state, theStore.zip]; 
      storeWebsite.text = [NSString stringWithFormat:@"%@", theStore.website]; 
      } 

} 

回答

1

只是爲了解釋爲什麼你看到「的方法加載所有數據」:

if條件使用單個等於(=)符號而不是雙倍(==)。
單個=是一個賦值,而雙==是一個用於檢查相等性的賦值。

由於指定在所有if中成功執行,所有if中的代碼都會執行。


然而,真正的問題是,你正在創建這是不是你想要的didSelectAnnotationView委託方法的註釋視圖的新實例。

您可以使用該方法提供給您的註釋視圖實例作爲參數(即view),而不是創建新實例。

因此理論上你可以看看view.tag

但我強烈建議不要依賴標籤,而是將所有需要註解的數據放在註釋類本身中。然後,您可以使用此方法使用view.annotation訪問註釋並將其轉換爲您的自定義類以訪問自定義屬性:

MyAnnotationClass *myAnn = (MyAnnotationClass *)view.annotation; 
NSLog(@"myAnn.someCustomProperty = %@", myAnn.someCustomProperty); 
相關問題