基本上,我使用一个包含地点对象数组的(模型)数组。对于每个地点对象,我都有各种属性,例如纬度、经度、地名、地点描述等。
我可以在这些地方放置注释。但是,当用户单击右侧的调用披露按钮时,我希望他们转到另一个 View Controller 。现在我知道该怎么做了。
我遇到的严重问题是我无法将单击的注释与我的模型数组链接起来。在我的下一个 View Controller 中,我将显示所有地点信息,但我无法发现单击了哪个注释。
考虑一个具有索引 path.row 的 TableView ,我需要类似的东西来发现正确的注释。
谢谢。
-(void)setMapAnnotations
{
//Create the region - N.B - part of the map you wish to show
MKCoordinateRegion dealMapRegion;
//Center - N.B - The exact center of the region.
CLLocationCoordinate2D center;
center.latitude = 51.481623;
center.longitude = -0.18519;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
dealMapRegion.center = center;
dealMapRegion.span = span;
[self.dealMapMapView setRegion:dealMapRegion animated:YES];
//array to hold places
NSMutableArray *placeLocations = [[NSMutableArray alloc]init];
CLLocationCoordinate2D location;
//After region has been set you need to populate the annotations
for (DealListModel *dealLstObj in self.dealMapModelArray) {
//Get the place from the model objects stored in the model array
PlaceDealAnnotation *placeAnnontation = [[PlaceDealAnnotation alloc]init];
double tempPlaceLatitude = [dealLstObj.placeLatitude doubleValue];
double tempPlaceLongitude = [dealLstObj.placeLongitude doubleValue];
location.latitude = tempPlaceLatitude;
location.longitude = tempPlaceLongitude;
placeAnnontation.coordinate = location;
placeAnnontation.title = dealLstObj.placeDescription;
//adding array of locations map
[placeLocations addObject:placeAnnontation];
}
[self.dealMapMapView addAnnotations:placeLocations];
}//end of setMapAnnotations
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
NSString *annotationIdentifier = @"CustomViewAnnontation";
DealMapCustomAnnotationView *customAnnotationView = (DealMapCustomAnnotationView *) [self.dealMapMapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!customAnnotationView) {
customAnnotationView = [[DealMapCustomAnnotationView alloc]initWithAnnotationwithImage:annotation reuseIdentifier:annotationIdentifier annotationViewImage:[UIImage imageNamed:@"pin.png"]];
customAnnotationView.canShowCallout = YES;
customAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
return customAnnotationView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[self performSegueWithIdentifier:@"mapPlaceSegue" sender:view];
}
最佳答案
将注释与模型数组关联的许多解决方案。
以下解决方案是我使用的:
1.在 PlaceDealAnnotation 中添加 int 变量“tag”
@property (nonatomic, assign) int tag;
2.更改setMapAnnotations:方法中的“for循环”
//After region has been set you need to populate the annotations
for (int tag = 0; tag < [self.dealMapModelArray count]; tag++) {
//Get the place from the model objects stored in the model array
DealListModel *dealLstObj = [self.dealMapModelArray objectAtIndex:tag];
PlaceDealAnnotation *placeAnnontation = [[PlaceDealAnnotation alloc]init];
double tempPlaceLatitude = [dealLstObj.placeLatitude doubleValue];
double tempPlaceLongitude = [dealLstObj.placeLongitude doubleValue];
//set tag here
placeAnnontation.tag = tag;
location.latitude = tempPlaceLatitude;
location.longitude = tempPlaceLongitude;
placeAnnontation.coordinate = location;
placeAnnontation.title = dealLstObj.placeDescription;
//adding array of locations map
[placeLocations addObject:placeAnnontation];
}
3.实现以下mapView的委托(delegate)方法:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
PlaceDealAnnotation * annotation = [view annotation];
int tag = annotation.tag;
//get the model your want here
DealListModel *targetModel = [self.dealMapModelArray objectAtIndex:tag];
//go to the next view controller here
}
关于ios - 将注释链接到模型数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20226054/