MKPinAnnotationView立即加载didSelectAnnotationView方法中的所有数据

时间:2012-08-13 23:25:26

标签: ios mkmapview mkannotation

我一直在探索MKMapView很长一段时间试图更熟悉它,我遇到了一个问题。

我的MapView填充了两个引脚,当我按下它们时,它们有各自的注释。我还有一个按钮,它将带我到一个带有UlLabels的新UIView,它将从数组中加载数据。

看着我的控制台,我注意到数据正在通过,但它不是仅仅针对我选择加载它们所有的引脚然后显示最后一个。

以下是我用于加载下一个视图数据的方法的代码:

- (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 个答案:

答案 0 :(得分:1)

只是解释为什么你会看到“加载所有数据的方法”:

if条件使用单个等号(=)符号而不是双倍(==)。
=是一个赋值,而双==是用于检查相等的那个。

由于赋值在所有if中成功执行,因此所有if内的代码都会执行。


但是,真正的问题是您在didSelectAnnotationView委托方法中创建了一个新的注释视图实例,这不是您想要的。

您可以使用该方法为您提供的注释视图实例(即view),而不是创建新实例。

理论上你可以看一下view.tag

但我强烈建议不要依赖标签,而是将注释所需的所有数据放在注释类本身中。然后,您可以使用view.annotation访问此方法中的注释,并将其强制转换为自定义类以访问自定义属性:

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