problem with MKMapView and navigation controller
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:object开发者_如何学运维MapVC animated:YES];
[objectMapVC release];
Any help greatly appreciated!
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.
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.
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.
精彩评论