如何在分屏视图ipad编程中实时添加MKMap上的pin注释



我在Split-View base的Detailpane中显示MKMap上的注释时遇到了麻烦

我有一个表在主窗格(左)(弹出窗口),当我在主窗格中选择一行,详细窗格(右)应该有一个注释出现,但它不是。

SecondViewController.m中didSelectRowAtIndexPath函数中的代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DetailViewController *dTVC = [[DetailViewController alloc] init];
    Lat = 15.1025;
    Long = 100.2510;
    [dTVC setShowAnnotation];
}

注意,Lat和Long是全局变量

DetailViewController.m中setShowAnnotation函数中的代码

- (void)setShowAnnotation {
    [_mapView removeAnnotation:self.customAnnotation];
    self.customAnnotation = [[[BasicMapAnnotation alloc] initWithLatitude:Lat andLongitude:Long] autorelease];
    [_mapView addAnnotation:self.customAnnotation];
}

当我选择行什么都没有发生时,我必须将函数setShowAnnotion链接到一个按钮,在选择行之后,什么都没有发生,必须按下那个按钮,注释将出现。我错过了什么?

可能它不起作用的主要原因是在didSelectRowAtIndexPath中,您正在创建DetailViewController的新实例,而不是使用已经显示的实例。

你创建的新实例也没有被显示(呈现),所以即使setShowAnnotation可能正在工作,你也无法在屏幕上看到任何东西。

在标准的基于分割视图的应用程序中,左边的主窗格被称为RootViewController(你正在调用的是SecondViewController)。在标准模板中,已经有一个detailViewController ivar,它指向当前显示的DetailViewController实例。

你可能想要使用标准模板从头开始重新启动你的应用程序,尽可能少地做更改并使用预编码的功能。或者,使用标准模板创建一个新项目,并研究它是如何工作的,并相应地修改您的项目。

然后,在didSelectRowAtIndexPath中,不是创建DetailViewController的新实例,而是在ivar上调用setShowAnnotation:
[detailViewController setShowAnnotation];


我建议的另一件事是,不要使用全局变量将坐标"传递"给细节视图,而是将纬度和经度作为参数添加到setShowAnnotation方法本身。

所以方法声明应该是这样的:

- (void)setShowAnnotationWithLat:(CLLocationDegrees)latitude 
                         andLong:(CLLocationDegrees)longitude;

,它会像这样被调用:

[detailViewController setShowAnnotationWithLat:15.1025 andLong:100.251];

最新更新