GestureRecognizer:将地图上的引脚放置在SIGABRT中

时间:2016-02-07 12:20:14

标签: ios swift swift2 unrecognized-selector apple-maps

我正在尝试向地图添加手势识别器;但是,当我启动程序时,它会发出信号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

的委托

2 个答案:

答案 0 :(得分:2)

func应命名为handleLongPress,而不是命名为action

即,

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)
}

答案 1 :(得分:1)

由于MKMapView已经有手势识别器,请尝试实现UIGestureRecognizerDelegate的这种方法:

@post = Post.joins(:user).where(posts: { user_id: current_user.id }).last