为什么我无法在前一个视图控制器上查看结构值

发布于 2024-11-07 18:10:57 字数 1255 浏览 0 评论 0原文

我对 Objective-C(以及 C 本身)还很陌生,所以这可能真的很容易,但我有点卡住了。

我有一个视图控制器,需要用户的一些输入,我有一个表视图,其中有一行用于纬度和经度值。当用户点击该行时,会将他们带到一个带有地图视图的新视图控制器。

我有一个标记供他们拖动并挑选出他们的位置:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView 
didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState 

在这种方法中,我想用新坐标更新以前的视图控制器。

它有一个属性:

CLLocationCoordinate2D destinationCoordinates;

我已将其设置为非原子、分配和合成

我的代码执行此操作:

NSArray *allControllers = self.navigationController.viewControllers;

EnterDetailsViewController *parent = [allControllers objectAtIndex:([allControllers count] -2)];

if ([parent isKindOfClass:[EnterDetailsViewController class]])
{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = annotation.coordinate.latitude;
    theCoordinate.longitude =  annotation.coordinate.longitude;

    parent.destinationCoordinates = theCoordinate;

    NSLog(@"Set coord to %@ - %@",
          parent.destinationCoordinates.latitude, 
          parent.destinationCoordinates.latitude);

但是 NSLog 总是因 EXC_BAD_ACCESS 崩溃 - 这是为什么?我在这里误解了什么核心概念?

I'm pretty new to Objective-C (and C itself) so this is probably really easy, but I'm a bit stuck.

I've got a view controller that requires some input from the user, I have a table view that has a row for a latitude and longitude value. When the user taps that row, it takes them to a new view controller with a map view on it.

I've got a marker for them to drag and pick out their location:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView 
didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState 

In this method I want to update the previous view controller with the new coordinates.

It has a property:

CLLocationCoordinate2D destinationCoordinates;

which I've set as nonatomic, assign and synthesized

My code does this:

NSArray *allControllers = self.navigationController.viewControllers;

EnterDetailsViewController *parent = [allControllers objectAtIndex:([allControllers count] -2)];

if ([parent isKindOfClass:[EnterDetailsViewController class]])
{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = annotation.coordinate.latitude;
    theCoordinate.longitude =  annotation.coordinate.longitude;

    parent.destinationCoordinates = theCoordinate;

    NSLog(@"Set coord to %@ - %@",
          parent.destinationCoordinates.latitude, 
          parent.destinationCoordinates.latitude);

But the NSLog always crashes with an EXC_BAD_ACCESS - why is this? What core concept am I misunderstanding here?

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

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

发布评论

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

评论(1

海风掠过北极光 2024-11-14 18:10:57

CLLocationCooperative2D 的定义是:

typedef struct {
  CLLocationDegrees latitude;
  CLLocationDegrees longitude;
} CLLocationCoordinate2D;

您使用 %@ 来打印对象,请尝试改为:

NSLog(@"Set coord to %f - %f",
      parent.destinationCoordinates.latitude,
      parent.destinationCoordinates.longitude);

注意 CLLocationDegrees 的类型为 double:

typedef double CLLocationDegrees;

The definition of CLLocationCoordinate2D is:

typedef struct {
  CLLocationDegrees latitude;
  CLLocationDegrees longitude;
} CLLocationCoordinate2D;

Your using %@ to print an object, try instead:

NSLog(@"Set coord to %f - %f",
      parent.destinationCoordinates.latitude,
      parent.destinationCoordinates.longitude);

Note that CLLocationDegrees is of type double:

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