iPhone:从另一个视图调用时 addAnnotation 不起作用

发布于 2024-09-02 19:26:26 字数 1134 浏览 7 评论 0原文

我有两个视图,第一个视图上有一个名为ridesMap 的MKMapView。第二个视图只是一个带有 UITableView 的视图。当您单击第二个视图中的“保存”按钮时,它会调用第一个视图中的方法:

// Get my first views class
MyRidesMapViewController *rideMapView = [[MyRidesMapViewController alloc] init];
// Call the method from my first views class that removes an annotation
[rideMapView addAnno:newRidePlacemark.coordinate withTitle:rideTitle.text withSubTitle:address];

这正确地调用了 addAnno 方法,如下所示:

- (void)addAnno:(CLLocationCoordinate2D)anno withTitle:(NSString *)annoTitle withSubTitle:(NSString *)subTitle {

    Annotation *ano = [[[Annotation alloc] init] autorelease];

    ano.coordinate = anno;

    ano.title = annoTitle;

    ano.subtitle = subTitle;

    if ([ano conformsToProtocol:@protocol(MKAnnotation)]) {

        NSLog(@"YES IT DOES!!!");

    }

    [ridesMap addAnnotation:ano];

}//end addAnno

此方法创建了一个符合 MKAnnotation 的注释,并且它假设将该注释添加到使用 addAnnotation 方法的地图。但是,注释永远不会被添加。

当未添加注释时,我永远不会收到任何错误。但当调用该方法时它永远不会出现。

为什么会这样呢?看来我已经正确完成了所有操作,并且将正确的 MKAnnotation 传递给了 addAnnotation 方法。所以,我不明白为什么它从来不掉针?难道是因为我从另一个视图调用这个方法?为什么这很重要?

I have two views, the first view has an MKMapView on it named ridesMap. The second view is just a view with a UITableView in it. When you click the save button in the second view, it calls a method from the first view:

// Get my first views class
MyRidesMapViewController *rideMapView = [[MyRidesMapViewController alloc] init];
// Call the method from my first views class that removes an annotation
[rideMapView addAnno:newRidePlacemark.coordinate withTitle:rideTitle.text withSubTitle:address];

This correctly calls the addAnno method, which looks like:

- (void)addAnno:(CLLocationCoordinate2D)anno withTitle:(NSString *)annoTitle withSubTitle:(NSString *)subTitle {

    Annotation *ano = [[[Annotation alloc] init] autorelease];

    ano.coordinate = anno;

    ano.title = annoTitle;

    ano.subtitle = subTitle;

    if ([ano conformsToProtocol:@protocol(MKAnnotation)]) {

        NSLog(@"YES IT DOES!!!");

    }

    [ridesMap addAnnotation:ano];

}//end addAnno

This method creates an annotation which does conform to MKAnnotation, and it suppose to add that annotation to the map using the addAnnotation method. But, the annotation never gets added.

I NEVER get any errors when the annotation does not get added. But it never appears when the method is called.

Why would this be? It seems that I have done everything correctly, and that I am passing a correct MKAnnotation to the addAnnotation method. So, I don't get why it never drops a pin? Could it be because I am calling this method from another view? Why would that matter?

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

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

发布评论

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

评论(1

素罗衫 2024-09-09 19:26:26

您正在创建 MyRidesMapViewController 的第二个实例。您应该将注释添加到原始实例中。您需要提供一些将该实例传递到第二个视图的方法。有很多可能的方法可以做到这一点;最佳选择取决于您的应用程序的结构(我们不知道)。

You are creating a second instance of MyRidesMapViewController. You should be adding the annotation to the original instance. You need to provide some means of passing that instance to your second view. There are many possible ways of doing this; the optimum choice depends on how your app is structured (which we don't know).

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