【问题标题】:MKPinAnnotationView loading all data in the didSelectAnnotationView method at onceMKPinAnnotationView 一次性加载 didSelectAnnotationView 方法中的所有数据
【发布时间】:2012-08-13 23:25:26
【问题描述】:

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

我的 MapView 填充了两个引脚,当我按下它们时,它们有各自的注释。我还有一个按钮,可以将我带到一个带有 UlLabels 的新 UIView,该 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];  
            }

}

【问题讨论】:

    标签: ios mkmapview mkannotation


    【解决方案1】:

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

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

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


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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多