我试图通过触摸添加注释到mapview上,在我发布新闻后我得到了这个错误:
[MKPointAnnotation index]:无法识别的选择器发送到实例
有谁能让我知道原因是什么?
代码:
- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
{
return;
}
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
CLLocationCoordinate2D touchMapCoordinate = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = touchMapCoordinate;
point.title = @"Test";
point.subtitle = @"Test2";
for (id annotation in self.mapView.annotations) {
[self.mapView removeAnnotation:annotation];
}
[self.mapView addAnnotation:point];
}
以下是我在 ViewDidLoad 方法中的内容:
if (_isSelectLocation)
{
UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
longPressGestureRecognizer.minimumPressDuration = 0.5;
[self.mapView addGestureRecognizer:longPressGestureRecognizer];
}
添加了堆栈跟踪:
Stack trace : (
0 Lookcounter 0x0000000100145680 -[MapViewController handleLongPress:] + 1016
1 UIKit 0x000000018a2ceec4 <redacted> + 276
2 UIKit 0x000000018a168508 <redacted> + 580
3 UIKit 0x000000018a5d9214 <redacted> + 60
4 UIKit 0x000000018a12c26c <redacted> + 292
5 UIKit 0x000000018a12a618 <redacted> + 2504
6 CoreFoundation 0x0000000185673ff0 <redacted> + 32
7 CoreFoundation 0x0000000185670f7c <redacted> + 360
8 CoreFoundation 0x000000018567135c <redacted> + 836
9 CoreFoundation 0x000000018559cf74 CFRunLoopRunSpecific + 396
10 GraphicsServices 0x000000018eff76fc GSEventRunModal + 168
11 UIKit 0x000000018a19ed94 UIApplicationMain + 1488
12 Lookcounter 0x00000001001418d0 main + 124
13 libdyld.dylib 0x0000000197a32a08 <redacted> + 4
)
答案 0 :(得分:1)
我不确定是什么问题。我复制了所有代码,效果很好。我把它作为一个项目发布在github上。
https://github.com/zhangjianxing/trySelectorApp
我相信还有其他原因导致编译器无法识别选择器(在swift中如果将“handleLongPress”设置为私有,则会出现相同的错误)。
我建议您评论其他方法/属性,然后重试。