我在Xcode上使用MapView,一切都很好,但当我添加以下行时
mapView.delegate = self;
到ViewController.m,我得到错误
正在分配给'id<MKMapViewDelegate>"来自不兼容的类型"ViewController*常量__强
这是我的代码:
ViewController.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.showsUserLocation = YES;
mapView.delegate = self; //The line I added that creates the error
// Do any additional setup after loading the view, typically from a nib.
}
@end
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController {
MKMapView *mapview;
}
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
您需要声明您的类实现了MKMapViewDelegate方法。在ViewController.h头文件中,更改行
@interface ViewController : UIViewController {
至
@interface ViewController : UIViewController <MKMapViewDelegate> {
您需要声明您响应了MKMapViewDelegate
调用。
要做到这一点,只需更新相应类(示例中为ViewController.h
)的头文件,如下所示:
@interface ViewController : UIViewController <MKMapViewDelegate> {