【问题标题】:Loading data in Views depending on MapKit annotation callout click根据 MapKit 注释标注在视图中加载数据单击
【发布时间】:2010-08-25 01:42:22
【问题描述】:

我有一张地图,其注释的标注上有一个披露按钮,单击该按钮时会显示详细视图。地图上的每个点都需要在详细视图中加载不同的数据——出于显而易见的原因,我不想为每个注释构建单独的视图,但我不确定执行此操作的最佳方法。

如何标记已单击的注释,以便通过详细信息的视图控制器加载正确的数据?

【问题讨论】:

    标签: iphone objective-c views mapkit


    【解决方案1】:

    您可以在注解类中实现数据加载功能。为此,您需要在注释类中引用该数据。

    你的注释类可以是这样的

    @interface MyAnnotation : NSObject<MKAnnotation> {
         CLLocationCoordinate2D coordinate;
    
         MyDataObject *mydataObject;
    }
    
    - (id) initWithCoordinate:(CLLocoationCoordinate2D)coord withMyData:(MyDataObject*)aDataObject;
    - (void) loadDetailView;   // this function will load the data in myDataObject to the DetailView
    

    要获得标注触摸,您可以在实现 MKMapViewDelegate 的控制器中实现 calloutAccessaryControlTapped 函数。

    函数类似于

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
          MyAnnotation *myAnnotation = (MyAnnotation*)view.annotation;
          [myAnnotation loadDetailView];
    }
    

    编辑

    initWithCoordinate 的实现类似于:

    @implementation MyAnnotation
    
    ....
    
    - (id) initWithCoordinate:(CLLocoationCoordinate2D)coord withMyData:(MyDataObject*)aDataObject{
        coordinate = coord;    // cannot use "self.coordinate" here as CLLocationCoordinate2D is double instead of object (?)
    
        self.title = <# some name #> // assign some string to the title property
    
        self.mydataObject = aDataObject; // this is your data object
    
        return self;
    }
    
    ....
    

    【讨论】:

    • 谢谢 tmin。请原谅我的无知,但我找不到有关解决方案数据部分的任何文档。 withMyData: 是标准函数吗?你能解释一下数据函数是如何工作的吗?我不明白它是如何使用的......
    • withMyData: 不是标准函数参数。我包含该参数以传递数据对象引用,以便稍后在 loadDataView 方法中使用它。请参阅我对 initWithCoordinate 函数实现的编辑。您也可以通过 google 了解如何使用 MKAnnotation 和 MKMapView。
    猜你喜欢
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多