将 UINavigationController 的坐标获取到 showDetails 方法中

发布于 2024-10-01 09:38:12 字数 1734 浏览 2 评论 0原文

我有一个地图视图,其中包含引脚注释。当用户按下 pin 信息的公开按钮时,我需要将 pin 的坐标传递到详细视图。当我进入 showDetails 方法时,如何获取 pin 的坐标?我正在使用下一个代码。

- (void)showDetails:(id)sender {

 DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil ];

    // HERE I NEED TO PASS THE COORDINATES OF THE PIN TO THE DETAILVIEWCONTROLLER.
    [self.navigationController pushViewController:detailViewController animated:YES];
 [detailViewController release];
}


- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {

 // If it's the user location, just return nil.
 if ([annotation isKindOfClass:[MKUserLocation class]])
  return nil;
 else { // Handles the other annotations.
  // Try to dequeue an existing pin view first.
  static NSString *AnnotationIdentifier = @"AnnotationIdentifier";
  MKPinAnnotationView *pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];

  if (!pinView) {
   // If an existing pin view was not available, creates one.
   MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
   customPinView.animatesDrop = YES;
   customPinView.canShowCallout = YES;

   // Adds a detail disclosure button.
   UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
   [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
   customPinView.rightCalloutAccessoryView = rightButton;

   return customPinView;
  } else
   pinView.annotation = annotation;
 }

 return nil;
}

感谢您的阅读。

I have a map view with pins annotations into it. I need to pass to the detail view the coordinates of a pin when the user press the disclosure button of the pin information. How can I get the coordinates of the pin when I am into the showDetails method? I am using the next code.

- (void)showDetails:(id)sender {

 DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil ];

    // HERE I NEED TO PASS THE COORDINATES OF THE PIN TO THE DETAILVIEWCONTROLLER.
    [self.navigationController pushViewController:detailViewController animated:YES];
 [detailViewController release];
}


- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {

 // If it's the user location, just return nil.
 if ([annotation isKindOfClass:[MKUserLocation class]])
  return nil;
 else { // Handles the other annotations.
  // Try to dequeue an existing pin view first.
  static NSString *AnnotationIdentifier = @"AnnotationIdentifier";
  MKPinAnnotationView *pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];

  if (!pinView) {
   // If an existing pin view was not available, creates one.
   MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
   customPinView.animatesDrop = YES;
   customPinView.canShowCallout = YES;

   // Adds a detail disclosure button.
   UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
   [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
   customPinView.rightCalloutAccessoryView = rightButton;

   return customPinView;
  } else
   pinView.annotation = annotation;
 }

 return nil;
}

Thanks for reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捂风挽笑 2024-10-08 09:38:12

我现在无法为您提供有效的代码解决方案,因为我这里没有我的 Mac。但我可以告诉你一种可能的方法。

在 DetailViewController 中创建一个方法来填充其属性,以便您可以在推送之前从 showDetails 调用它。所以事件的顺序会是这样的。

点击披露按钮->显示详细信息称为 ->创建并填充 DetailViewController ->推送 DetailViewController

希望这对您有帮助

I cannot give you a working code solution right now since i don't have my mac here. But I can tell you about a possible approach.

Create a method in DetailViewController that populates its attributes, so you can call it from showDetails, before you push it. So the sequence of events would be like this.

Click on disclosure button -> showDetails called -> DetailViewController created and populated -> push DetailViewController

Hope this helped you

忆悲凉 2024-10-08 09:38:12

您可以像这样自定义 UIButton 按钮:

@interface CustomButton : UIButton {

CLLocationCooperative2D pinCooperative;
现在

在你的 viewForAnnotation 中:

// Adds a detail disclosure button.
CustomButton *rightButton = [CustomButton buttonWithType:UIButtonTypeDetailDisclosure];
rightButton.pinCoordinate = annotation.coordinate
[rightButton addTarget:self action:@selector(showDetails:)   forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;

最后在 showDetails 方法中获取坐标:

CLLocationCoordinate2D currentCoord = ((CustomButton*)sender).pinCoordinate;

You can your UIButton button custom like this :

@interface CustomButton : UIButton {

CLLocationCoordinate2D pinCoordinate;
}

Now in your viewForAnnotation :

// Adds a detail disclosure button.
CustomButton *rightButton = [CustomButton buttonWithType:UIButtonTypeDetailDisclosure];
rightButton.pinCoordinate = annotation.coordinate
[rightButton addTarget:self action:@selector(showDetails:)   forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;

And finally to get the coordinate in the showDetails method :

CLLocationCoordinate2D currentCoord = ((CustomButton*)sender).pinCoordinate;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文