GestureRecognizer: размещение булавки на карте приводит к SIGABRT

Я пытаюсь добавить на карту распознаватель жестов; но, когда я запускаю программу, она вылетает с сигналом SIGABRT. Можете ли вы помочь мне разобраться, потому что я действительно новичок в разработке iOS?

override func viewDidLoad() {
    super.viewDidLoad()
    v.hidden=true
    let lpgr = UILongPressGestureRecognizer(target: self, action:"handleLongPress:")
    lpgr.delegate=self
    lpgr.minimumPressDuration = 0.5
    lpgr.delaysTouchesBegan = true
    self.theMap.addGestureRecognizer(lpgr)
}

func action(gestureRecognizer:UIGestureRecognizer){
    var touchPoint = gestureRecognizer.locationInView(theMap)
    var newCoordinates = theMap.convertPoint(touchPoint,   toCoordinateFromView: theMap)
    let annotation = MKPointAnnotation()
    annotation.coordinate = newCoordinates
    theMap.addAnnotation(annotation)
}
2016-02-07 13:30:17.963 SM[25924:2977301] -[SM.ViewController3 handleLongPress:]: unrecognized selector sent to instance 0x7f882a66d4d0
2016-02-07 13:30:17.968 SM[25924:2977301] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SM.ViewController3 handleLongPress:]: unrecognized selector sent to instance 0x7f882a66d4d0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010b046e65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010d042deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010b04f48d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010af9c90a ___forwarding___ + 970
    4   CoreFoundation                      0x000000010af9c4b8 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010c007e73 _UIGestureRecognizerSendTargetActions + 153
    6   UIKit                               0x000000010c0044e5 _UIGestureRecognizerSendActions + 162
    7   UIKit                               0x000000010c0024e2 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 843
    8   UIKit                               0x000000010c00a9a0 ___UIGestureRecognizerUpdate_block_invoke904 + 79
    9   UIKit                               0x000000010c00a83e _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342
    10  UIKit                               0x000000010bff8101 _UIGestureRecognizerUpdate + 2634
    11  CoreFoundation                      0x000000010af72367 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    12  CoreFoundation                      0x000000010af722d7 __CFRunLoopDoObservers + 391
    13  CoreFoundation                      0x000000010af67f2b __CFRunLoopRun + 1147
    14  CoreFoundation                      0x000000010af67828 CFRunLoopRunSpecific + 488
    15  GraphicsServices                    0x000000010e75cad2 GSEventRunModal + 161
    16  UIKit                               0x000000010bb1f610 UIApplicationMain + 171
    17  SM                                  0x000000010add1a8d main + 109
    18  libdyld.dylib                       0x000000010f0b192d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Сам класс делегирован GestureRecognizer.


person Andre Liberty    schedule 07.02.2016    source источник
comment
опубликуйте фактическое сообщение об ошибке и func handleLongPress(sender:....).   -  person R Menke    schedule 07.02.2016
comment
Пожалуйста, опубликуйте весь код, включая функцию handleLongPress.   -  person Tapani    schedule 07.02.2016


Ответы (2)


func должен называться handleLongPress, а не action:

I.e.,

func handleLongPress(gestureRecognizer:UIGestureRecognizer)
{
     var touchPoint = gestureRecognizer.locationInView(theMap)
     var newCoordinates = theMap.convertPoint(touchPoint, toCoordinateFromView: theMap)
     let annotation = MKPointAnnotation()
     annotation.coordinate = newCoordinates
     theMap.addAnnotation(annotation)
}
person Unheilig    schedule 07.02.2016

Поскольку в MKMapView уже есть распознаватели жестов, попробуйте реализовать этот метод UIGestureRecognizerDelegate:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
    shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
    return true
}
person Tapani    schedule 07.02.2016