Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKMapView not calling delegate methods

Tags:

iphone

mapkit

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.

like image 478
criscokid Avatar asked Sep 04 '09 13:09

criscokid


5 Answers

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.

like image 55
void Avatar answered Oct 06 '22 02:10

void


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
like image 22
user2057434 Avatar answered Oct 23 '22 11:10

user2057434


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.

like image 9
Blackbird Avatar answered Oct 23 '22 12:10

Blackbird


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???

like image 4
Yoichi Avatar answered Oct 23 '22 11:10

Yoichi


maybe quite obvious, but just checking:

Have you made sure your viewcontroller declaration is correctly done. Something like:

@interface YourViewController : UIViewController <MKMapViewDelegate>
like image 1
prakash Avatar answered Oct 23 '22 10:10

prakash