0

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

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

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

这是我用来为下一个视图加载数据的方法的代码:

- (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];  
            }

}
4

1 回答 1

1

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

if条件是使用单个等号 ( )而=不是双 ( ==)。
single=是一个分配,而 double==是用于检查相等性的一个。

由于赋值在所有ifs 中都成功执行,所以所有 s 内部的代码都会if执行。


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

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

所以理论上你可以看看view.tag.

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

MyAnnotationClass *myAnn = (MyAnnotationClass *)view.annotation;
NSLog(@"myAnn.someCustomProperty = %@", myAnn.someCustomProperty);
于 2012-08-14T00:43:48.677 回答