问题
In a UIViewController I add a MKMapView to the view controlled by the controller.
- (void)viewDidLoad {
[super viewDidLoad];
CGRect rect = CGRectMake(0, 0, 460, 320);
map = [[MKMapView alloc] initWithFrame:rect];
map.delegate = self;
[self.view addSubview:map];
}
Later in the controller I have
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
NSLog(@"done.");
}
Done never gets printed. None of the other delegate methods get called either like mapView:viewForAnnotation: I use a MKMapView in an another app, but this seems to happen on any new application I make. Has anyone else seen this behavior?
EDIT:
The problem seems to be when UIViewController is made the delegate of the MKMapView, a direct subclass of NSObject seems to work okay. I can work around like this, still seems very odd since I've done it before.
回答1:
I had the exact same problem: I had assigned < MKMapViewDeledate> to my controller, and linked the delegate to "File owner" in Interface Builder, but still my implemented delegate methods were not called.
The reason is that since iOS 4, maps are cached, and whenever an app display a cached map then methods such as "mapViewDidFinishLoadingMap" are not called. Resetting "contents and settings" in the simulator clears the cached maps and therefore solves the issue.
回答2:
A bit old, but since iOS 7 there is a new method that might work. solved my problem
- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered
回答3:
Perhaps you need to go to the IB and control-drag from the MKMapView to the view conroller, then select delegate to make it a delegate???
回答4:
maybe quite obvious, but just checking:
Have you made sure your viewcontroller declaration is correctly done. Something like:
@interface YourViewController : UIViewController <MKMapViewDelegate>
回答5:
I had a similar problem with the methods of MKMapViewDelegate not being called. My issue was setting the MKCoordinateRegionMakeWithDistance and regionThatFits in the controller's -viewDidLoad() I wanted to only show the region around my house and not start with the world view. So after adding annotations in the controller's viewDidLoad, I started a timer. When it expires in one second, I zoom in on the region I want with the above APIs and the delegate methods fire. It just makes me a little dizzy and inclined to vomit on my iPad.
Now I only have to deal with the darn low memory warnings.
回答6:
I just ran into this again on iOS 7 and figured out something that works consistently. The issue we're typically fighting is calling setRegion before the region has context (view rendered). iOS 7 adds new delegate methods representing all tiles being rendered but pre-iOS 7 the cached tiles prevents the delegate method mapViewDidFinishLoadingMap from being called. Instead I use a "pendingRegion" variable to check if I have "queued" a region change for when the map is rendered and then simply use the viewDidAppear method as a signal the map has been rendered to the screen and hence a region will have context.
Add a variable to your header:
MKCoordinateRegion pendingRegion;
Add this method to your code:
-(void)initPendingRegion {
pendingRegion.center = CLLocationCoordinate2DMake(0.0, 0.0);
}
Add this to your viewDidAppear:
if(pendingRegion.center.latitude != 0.0) {
MKCoordinateRegion useRegion = pendingRegion;
[self initPendingRegion];
[self.mapView setRegion:useRegion animated:YES];
}
回答7:
I had a similar issue in XCode 6.1 building for iOS 8.1 where none of the MKMapViewDelegate methods were being called. In a 2nd application the identical code resulted in the MKMapViewDelegate methods being called as expected.
In ViewController.m the mapView delegate was set as follows:
- (void)viewDidLoad {
...
self.mapView.delegate = self;
In ViewController.h:
@interface myMapViewController : UIViewController <MKMapViewDelegate>
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@end
I had manually added the IBOutlet line above to the header file, but only in the app where the delegate methods were not being received.
The solution was to delete the IBOutlet line I had added manually to the header, and then go to the storyboard and CTRL-drag from the MKMapView to the @interface block in my ViewController.h. The identical IBOutlet was recreated in the header file with the result that all the delegate methods were called correctly.
回答8:
My problem is that I forget to set location in the simulator. Go to Simulator -> Debug -> Location -> Custom location...
and the MKMapViewDelegate
methods will start calling
回答9:
For me, it worked after adding NSLocationWhenInUseUsageDescription
string in Info.plist
回答10:
I also met same issue as yours. I found that: with the iPhone SDK 3.2 - when I create a new UIViewController with an associated xib file (the checkbox option in UIViewController creating dialog), delegate methods of MKMapViewDelegate are never called.
However, when I follow below steps, it runs well.
Create a new UIViewController class (never check the option: create xib file associated with the controller)
Create a new xib file. Add the map using Interface Builder + setting the Owner class with the class on step 1 + setting delegate object of the map point to the owner class.
Implement delegate methods for the UIViewController class in step 1.
When I want to use my ViewController (with the added map), I load it with the nib name as below example:
MapPreviewController* mapPreviewController = [[MapPreviewController alloc] initWithNibName:@"MapPreviewController" bundle:[NSBundle mainBundle]]; [self.centerPanelView addSubview:mapPreviewController.view];
The code runs ok. I don't know what Apple changes or did with the xib file when I create my UIViewController using the wizard, but it may the root cause of my problem. It takes me 1 day to find this stupid solution.
If you found another one, please share to me.
Thanks
来源:https://stackoverflow.com/questions/1379358/mkmapview-not-calling-delegate-methods