gpt4 book ai didi

ios - MKPinAnnotationView中如何实现多个标注

转载 作者:行者123 更新时间:2023-11-28 17:45:54 26 4
gpt4 key购买 nike

这是我关于堆栈溢出的第一个问题,我也是新的 iOS 开发人员。我正在为我的学校开发 map 应用程序。我的学校有 25 栋楼。我的 map 中有 25 个 MKPinAnnotationViews。

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;

// handle our two custom annotations
//
if ([annotation isKindOfClass:[SchureAnnotation class]]) // for Harry Schure Hall
{
// try to dequeue an existing pin view first
static NSString* SchureAnnotationIdentifier = @"schureAnnotationIdentifier";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)
[mapView dequeueReusableAnnotationViewWithIdentifier:SchureAnnotationIdentifier];
if (!pinView)
{
// if an existing pin view was not available, create one
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:SchureAnnotationIdentifier] autorelease];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;

// add a detail disclosure button to the callout which will open a new view controller page
//
// note: you can assign a specific call out accessory view, or as MKMapViewDelegate you can implement:
// - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
//
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;

return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
else if ([annotation isKindOfClass:[SaltenAnnotation class]]) // for Salten Hall
{
// try to dequeue an existing pin view first
static NSString* SaltenAnnotationIdentifier = @"saltenAnnotationIdentifier";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)
[mapView dequeueReusableAnnotationViewWithIdentifier:SaltenAnnotationIdentifier];
if (!pinView)
{
// if an existing pin view was not available, create one
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:SaltenAnnotationIdentifier] autorelease];
..........

我想放置一个不同的标注来显示每个引脚的信息。我正在调用函数 showDetails: 看起来像这样

- (void)showDetails:(id)sender
{
// the detail view does not want a toolbar so hide it
[self.navigationController setToolbarHidden:YES animated:NO];

[self.navigationController pushViewController:detailViewController animated:YES];
}

为了显示每栋楼的信息,我需要写25个view controller吗?或者有没有使用一个 View Controller ?如果我单击注释 View 的右键,它必须显示其信息(图片、文本)。如果我单击另一个注释,它必须通过替换旧注释来显示其信息。

非常感谢任何帮助。谢谢,普拉迪普。

最佳答案

不要使用[rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];

改为

[rightButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];

然后:

第一步:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[自行执行SegueWithIdentifier:@"TCServiceStationDetailFromMap"sender:view];
}

第 2 步:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

if ([segue.identifier isEqualToString:@"TCServiceStationDetailFromMap"]) {

    MKAnnotationView *view = sender;
TSPlaceMark *detailPlaceMark = (TSPlaceMark *)view.annotation;
//...
//so that you can get the different MKAnnotationView
}

关于ios - MKPinAnnotationView中如何实现多个标注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5979540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com