2015-08-17 34 views
2

我試圖通過觸摸即可添加註釋到一個MapView後,我鬆開按我得到這個錯誤:無法識別選擇[MKPointAnnotation指數]

[MKPointAnnotation index] : unrecognized selector sent to instance

誰能給我的原因是什麼的想法?

代碼:

- (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

在你的代碼中,你發送一個'index'消息給一個對象嗎?你發佈的代碼看起來很好。你還可以發佈整個堆棧跟蹤嗎? – Losiowaty

+0

我編輯了我的帖子以顯示它。那是你在找什麼? – linuxer

+0

是的,雖然編輯部分是奇怪的,沒有信息。您可以添加一個異常斷點(https://developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html)或者逐步調試您的處理程序,並告訴我們它究竟在哪一行發生故障?它總是發生,還是第二次/第三次/等觸摸?也在哪些設備(如果模擬器)和iOS版本你是否得到這些? – Losiowaty

回答

1

我不知道是什麼問題。我已經複製了您的所有代碼,並且運行良好。我把它作爲一個項目發佈在github上。

https://github.com/zhangjianxing/trySelectorApp

我相信有其他的事情的原因編譯器不認可的選擇(在SWIFT如果你設置「handleLongPress」是私有的,會出現同樣的錯誤)。

我建議您評論其他方法/屬性,然後重試。

+1

它最終成爲代表方法之一。特別是viewForAnnotation。 – linuxer

+0

它很好,你發現它。但爲什麼「viewForAnnotation」會導致「handleLongPress」無法識別。代理中是否有一些私有函數,名稱相同? –

+0

我在應用程序中使用自定義註釋用於其他目的,它具有通用註釋沒有的屬性。 – linuxer