首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS MapView Delegate 設定報錯:型別不相容解決方案

     編輯:程式設計綜合問答

錯誤訊息

Assigning to 'id<MKMapViewDelegate>' from incompatible type 'ViewController *const __strong'

發生於 mapView.delegate = self;,表示 ViewController 未遵循 MKMapViewDelegate 協定。

修正步驟

  1. 開啟 ViewController.h。
  2. 在介面宣告後加入協定:
    @interface ViewController : UIViewController <MKMapViewDelegate>
  3. (可選)在 ViewController.m 實作代理方法,如 - (void)mapView:didUpdateUserLocation: 等。

完整標頭檔範例

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end

補充說明

  • @synthesize mapView; 在現代 Xcode (LLVM 4.0+) 可省略,屬性會自動合成。
  • 若使用 Storyboard,確保 MKMapView 的 delegate Outlet 已連線至 View Controller。
  • iOS 6 起 MKMapView 預設顯示用戶位置需在 Info.plist 加入 NSLocationWhenInUseUsageDescription。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved