MKMapView 和导航控制器的问题

发布于 2024-08-28 18:48:10 字数 867 浏览 2 评论 0原文

我有一个 UITableView (在导航控制器堆栈上),它显示自定义模型对象的详细信息。该对象有一个数组属性来保存子对象,每个子对象都有纬度和经度属性。

由于我从 Web 服务 API 中“延迟加载”所有内容,因此直到用户第一次点击详细视图中的单元格以查看地图上的对象时,才会填充子对象数组。此时,我首先创建并推送一个临时“加载视图”,该视图进行 api 调用、填充数组,然后创建并推送地图视图。

问题是,一旦基于地图的视图被推送到导航控制器上,它的 MKMapView 就不会显示。导航栏标题和后退按钮更改正确,但我仍然可以看到应用程序选项卡栏和导航栏之间的先前加载视图! 如果该数组之前已填充,那么我将基于地图的视图直接推送到详细视图之后,并且工作正常。我也在我的项目中使用这个地图视图控制器,以前没有出现过问题。

我一生都无法理解这里发生了什么。如果我更改“加载”视图控制器以模态呈现地图视图,那么它也可以正常工作。地图视图控制器笔尖被 2 个自定义控制器类使用,我最初认为这是问题所在 - 但 IB 中的文件所有者现在设置为正确的控制器。

这是加载视图中推动地图视图的代码:

ObjectMapViewController *objectMapVC = [[ObjectMapViewController alloc] 
                initWithNibName:@"ObjectLocationView" bundle:nil];

objectMapVC.objectsToMap = self.object.childObjects;

[self.navigationController pushViewController:objectMapVC animated:YES];

[objectMapVC release];

非常感谢任何帮助!

I have a UITableView (on a navigation controller stack) which is showing details for a custom model object. The object has an array property to hold child objects which each have a latitude and longitude property.

As I am 'lazy-loading' everything from a web service API, the array of child objects is not populated until the first time the user taps a cell in the detail view to see the objects on a map. At this point I first create and push an interim 'loading view' which makes the api call, populates the array and then creates and pushes the map view.

The issue is once the map-based view is pushed onto the nav controller it's MKMapView is not showing. The nav bar title and back button change correctly but I can still see the previous loading view between the apps tab bar and nav bar!
If the array has been previously populated then I push the map-based view directly after the detail view and it works fine. I am also using this map view controller across my project with no previous problems.

I can't for the life of me get what's going on here. If I change the 'loading' view controller to present the map-view modally then it also works fine. The map view controllers nib was being used by 2 custom controller classes and I intially thought this was the problem - but the files owner in IB is set to the correct controller now.

Here's the code from the loading view which pushes the map view:

ObjectMapViewController *objectMapVC = [[ObjectMapViewController alloc] 
                initWithNibName:@"ObjectLocationView" bundle:nil];

objectMapVC.objectsToMap = self.object.childObjects;

[self.navigationController pushViewController:objectMapVC animated:YES];

[objectMapVC release];

Any help greatly appreciated!

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

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

发布评论

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

评论(3

李不 2024-09-04 18:48:11

出现此问题的最可能原因是您在后台线程中运行下载。如果在同一个线程中显示/配置 MapView,这可能是一个问题。所有 UI 操作都必须在主线程上运行。
这就解释了为什么performSelector afterteDelay 解决了这个问题。

The most likely reason for this problem is that you were running your download in a background thread. If in the same thread you show/configure your MapView, that could be an issue. All UI actions must run on main thread.
That would explain why the performSelector afteDelay solved the problem.

不奢求什么 2024-09-04 18:48:11

通过将上述代码包装在单独的方法中并

[self performSelector:@selector(pushMapView) withObject:nil afterDelay:0.3];

在 API 调用完成后调用 using 来修复此问题。

我假设导航控制器没有足够的时间来正确设置其视图堆栈,因为 API 调用通常需要不到一秒钟的时间才能完成,因此几乎在加载视图动画化后就推送了地图视图。

Fixed this by wrapping the above code in a seperate method and calling using

[self performSelector:@selector(pushMapView) withObject:nil afterDelay:0.3];

once the API call completes.

I'm presuming that the navigation controller didn't have enough time to properly setup its view stack as the API call generally takes under a second to complete so the map view was being pushed almost as soon as the loading view had animated in.

我相信我也有类似的问题。在与导航控制器和 MKMapView 斗争了一天之后,我终于找到了解决方案。这是我的代码

CustomMapController*mapController=[[[CustomMapController alloc]init] autorelease];
mapController.delegate=self;
mapController.view.userInteractionEnabled=YES;
UINavigationController*navController=[[[UINavigationController alloc]initWithRootViewController:mapController] autorelease];

[(UIViewController*)[self.view.superview nextResponder] presentModalViewController:navController animated:YES];
[mapController loadGPSMapWithlongitude:longitudeString andLatitude:latitudeString];

在函数 loadGPSMapWithlongitude 中,通过调用设置并重新绘制地图区域

[mapView setRegion:region animated:YES]; and 
[mapView regionThatFits:region];

我遇到的问题是,mapController 内的 mapView 从未被位置更新,即使我单步执行它以确保它正确执行并且所有的值确实都正确地传递给了mapview。

这是我的解决方案:

在 CustomMapController 中,覆盖该函数

 -(void)viewWillLayoutSubviews

并执行地图 updtae [mapView setRegion:self.regionanimated:YES];
[mapView regionThatFits:region];

所以在 loadGPDMapWithLongitude:: 函数中,我只是将位置传递给 self.region。由于一个非常奇怪的原因,UINavigationController + ModalView 内的mapview 必须遵循此代码顺序。除此之外我从来没有遇到过任何问题。

I believe I had a similar problem. After battling with the Navigation controller and MKMapView for a day, I finally found a solution. Here is my code

CustomMapController*mapController=[[[CustomMapController alloc]init] autorelease];
mapController.delegate=self;
mapController.view.userInteractionEnabled=YES;
UINavigationController*navController=[[[UINavigationController alloc]initWithRootViewController:mapController] autorelease];

[(UIViewController*)[self.view.superview nextResponder] presentModalViewController:navController animated:YES];
[mapController loadGPSMapWithlongitude:longitudeString andLatitude:latitudeString];

Within the function loadGPSMapWithlongitude, the region of the map is set and redrawn by calling

[mapView setRegion:region animated:YES]; and 
[mapView regionThatFits:region];

The issue I had was that the mapView inside the mapController never got updated by the location even though I stepped through it to ensure it was executed properly and all the values were indeed passed on correctly to mapview.

Here is my solution:

In side the CustomMapController, overwrite the function

 -(void)viewWillLayoutSubviews

and executes the map updtae [mapView setRegion:self.region animated:YES];
[mapView regionThatFits:region];

So in loadGPDMapWithLongitude:: function, I just passed on the location to self.region. For a very strange reason, mapview inside a UINavigationController + ModalView has to follow this code sequence. I never had any issue otherwise.

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