简体   繁体   中英

mapView:regionDidChangeAnimated: never called

In the storyboard I have a Navigation Controller in Relationship with a View Controller.

The View Controller contains a MKMapView.

The View Controller is also set, in Identity Inspector -> Custom Class, to be a custom controller: MapViewController.

This is MapViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MarkerAnnotation.h"
#import "MarkerButton.h"
#import "MainMapView.h"

@interface MapViewController : UITableViewController <MKMapViewDelegate, CLLocationManagerDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *localMapView;

@end

localMapView is linked to the MKMapView placed in the ViewController on the storyboard through "Referencing Outlets", and I'm using it already in the code on .m file for things like setting annotations or getting "bounds.origin".

Every time the user moves the map I need to run some lines to update the map. So, I tried to implement mapView:regionDidChangeAnimated: on the .m file, but this is never called.

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    NSLog(@"Region changed");
}

I tried creating a custom subclass of MKMapView implementing on that class the method, but wasn't called either. (you can still see in the imports of .h file, MainMapView.h... I still haven't deleted the import)

Is there something I'm missing? Do I have to set some more connection between the map and the current class?

Otherwise, is there another way I can trigger an event every time the map moves?

When delegate methods don't get called, first thing to check is whether the object's delegate property is set or connected.

(Declaring that a class implements the delegate protocol in the .h file is not the same thing -- that just lets the compiler know what should be expected of the class code implementation so it can provide compile-time warnings.)

So in addition to the referencing outlet, you must also connect the map view's delegate outlet to File Owner or in code (generally in viewDidLoad) do mapView.delegate = self; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM