![](/img/trans.png)
[英]Unrecognized selector sent to instance while adding annotation in MKMapview
[英]Unrecognized selector [MKCircle pointCount] when adding MKCircleOverlay to MKMapView
我正在使用Objective C開發適用於iOS 9.0和更高版本的應用程序。
該應用程序包括帶有MKMapView對象的視圖控制器。 視圖控制器是地圖視圖委托。
我在執行viewWillAppear:animated的實現中添加MKCircle疊加層時遇到問題。
使用addOverlay:level:
方法不能解決問題。
實現看起來像:
- (void)viewDidLoad {
[super viewDidLoad];
[self.mapView setDelegate:self];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.locationCircle = [MKCircle circleWithCenterCoordinate:self.locationPin.coordinate radius:1000000.0];
[self.mapView addOverlay:self.locationCircle];
}
- (MKOverlayRenderer *)rendererForOverlay:(id<MKOverlay>)overlay {
if ([overlay isKindOfClass:[MKCircle class]]) {
MKCircleRenderer *renderer = [[MKCircleRenderer alloc] initWithCircle:(MKCircle *)overlay];
renderer.fillColor = [[UIColor yellowColor] colorWithAlphaComponent:0.25];
return renderer;
} else {
return [self.superclass rendererForOverlay:overlay];
}
}
具體來說,地圖視圖嘗試顯示圓圈疊加層的任何部分時,應用都會崩潰。
該應用程序已成功在地圖視圖上渲染多邊形疊加層和各種圖釘注釋。 錯誤消息的詳細信息是:
2016-11-07 12:35:21.643 [MKCircle pointCount]: unrecognized selector sent to instance 0x6000004902c0
2016-11-07 12:35:21.645 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKCircle pointCount]: unrecognized selector sent to instance 0x6000004902c0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010cdc334b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010c82421e objc_exception_throw + 48
2 CoreFoundation 0x000000010ce32f34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x000000010cd48c15 ___forwarding___ + 1013
4 CoreFoundation 0x000000010cd48798 _CF_forwarding_prep_0 + 120
5 MapKit 0x0000000109af40a3 CreatePathForPolygon.38297 + 57
6 MapKit 0x0000000109af3e4c -[MKPolygonRenderer createPath] + 128
7 MapKit 0x0000000109aeefda -[MKOverlayPathRenderer drawMapRect:zoomScale:inContext:] + 72
8 MapKit 0x0000000109aefd89 __47-[MKOverlayRenderer overlay:drawKey:inContext:]_block_invoke + 671
9 MapKit 0x0000000109aefdda _worldsForBounds.37697 + 58
10 MapKit 0x0000000109aef99f -[MKOverlayRenderer overlay:drawKey:inContext:] + 224
11 VectorKit 0x00000001170e0534 __40-[VKRasterOverlayTileSource _queueDraw:]_block_invoke + 484
12 libdispatch.dylib 0x000000010de3a980 _dispatch_call_block_and_release + 12
13 libdispatch.dylib 0x000000010de640cd _dispatch_client_callout + 8
14 libdispatch.dylib 0x000000010de43499 _dispatch_queue_override_invoke + 1733
15 libdispatch.dylib 0x000000010de453b7 _dispatch_root_queue_drain + 720
16 libdispatch.dylib 0x000000010de4508b _dispatch_worker_thread3 + 123
17 libsystem_pthread.dylib 0x000000010e20d4de _pthread_wqthread + 1129
18 libsystem_pthread.dylib 0x000000010e20b341 start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
通過實驗,我發現,如果圓圈疊加層位於地圖上未顯示在屏幕上的一部分中,則可以添加圓圈疊加層而不會導致應用程序崩潰。 一旦用戶平移到地圖上圓圈所在的部分,就會發生崩潰。
將模擬器與iOS 10.1和iOS 9.0一起使用時,會發生錯誤。
對此原因的任何想法將不勝感激。
感謝大家的投入。 最后的問題是由於不正確的方法簽名:
- (MKOverlayRenderer *)rendererForOverlay:(id<MKOverlay>)overlay
應該是:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
這不會引起更大的問題,因為在代碼的其他地方,該方法的另一種實現具有正確的簽名。 該方法試圖將MKCircle視為MKPolygon,因此觸發了pointCount問題。
快速4.2。 添加以下代表。 我將Circle和PolyLine添加到MapView。 因此需要在委托中同時處理。
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKCircle {
let renderer = MKCircleRenderer(overlay: overlay)
renderer.fillColor = UIColor.black.withAlphaComponent(0.3)
renderer.strokeColor = UIColor.red
renderer.lineWidth = 1
return renderer
}
let polyLine = MKPolylineRenderer.init(overlay: overlay)
polyLine.strokeColor = UIColor.green
return polyLine
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.