Swift 在cocoa上处理鼠标事件时出错

Swift 在cocoa上处理鼠标事件时出错,swift,macos,cocoa,mouseevent,nsevent,Swift,Macos,Cocoa,Mouseevent,Nsevent,我有一个自定义的视图控制器,一个自定义的窗口控制器和一个自定义的视图。ViewController连接到WindowController,View连接到ViewController 在myViewController中,我有以下鼠标事件代码: override func mouseDown(with event: NSEvent) { //code } override func mouseUp(with event: NSEvent) { //code } 在启动时,

我有一个自定义的
视图控制器
,一个自定义的
窗口控制器
和一个自定义的
视图
ViewController
连接到
WindowController
View
连接到
ViewController

在my
ViewController
中,我有以下鼠标事件代码:

override func mouseDown(with event: NSEvent) { 
     //code
}

override func mouseUp(with event: NSEvent) {
     //code
}
在启动时,我收到以下错误消息,并且在调试时,甚至没有输入这些方法:

2018-03-08 11:27:11.594158+0100 MyProject[2697:83761] *** Assertion failure in -[NSEvent touchesMatchingPhase:inView:], /Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1504.83.101/AppKit.subproj/NSEvent.m:4654
2018-03-08 11:27:11.594574+0100 MyProject[2697:83761] [General] Invalid message sent to event "NSEvent: type=LMouseDown loc=(701.219,422.156) time=8788.4 flags=0 win=0x6080001e0200 winNum=1255 ctxt=0x0 evNum=1071 click=1 buttonNumber=0 pressure=1 deviceID:0x300000014400000 subtype=NSEventSubtypeTouch"
2018-03-08 11:27:11.599387+0100 MyProject[2697:83761] [General] (
    0   CoreFoundation                      0x00007fff8b59d2cb __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffa03b548d objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff8b5a2042 +[NSException raise:format:arguments:] + 98
    3   Foundation                          0x00007fff8cfeabe0 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   AppKit                              0x00007fff89327573 -[NSEvent touchesMatchingPhase:inView:] + 205
    5   MyProject                           0x0000000100013c0b _T015MyProject18MyViewControllerC9mouseDownySo7NSEventC4with_tF + 139
    6   MyProject                           0x0000000100014bbc _T015MyProject18MyViewControllerC9mouseDownySo7NSEventC4with_tFTo + 60
    7   AppKit                              0x00007fff891897a9 forwardMethod + 133
    8   AppKit                              0x00007fff891897a9 forwardMethod + 133
    9   AppKit                              0x00007fff8990424f -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 6341
    10  AppKit                              0x00007fff89900a6c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 1942
    11  AppKit                              0x00007fff898fff0a -[NSWindow(NSEventRouting) sendEvent:] + 541
    12  AppKit                              0x00007fff89784681 -[NSApplication(NSEvent) sendEvent:] + 1145
    13  AppKit                              0x00007fff88fff427 -[NSApplication run] + 1002
    14  AppKit                              0x00007fff88fc9e0e NSApplicationMain + 1237
    15  MyProject                           0x0000000100023add main + 13
    16  libdyld.dylib                       0x00007fffa0c9b235 start + 1
)
我已经尝试过以下方法:

  • 在WindowController中注册这两种方法,然后分别写入:

    让vc=self.nextResponder为!MyViewController vc.mouseUp//或vc.mouseDown

  • -->仍然得到上述错误

  • 通过编写以下代码,在
    viewDidLoad()/windowDidLoad()
    中注册这两个方法:

    NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown){
        self.mouseDown(with: $0)
        return $0 
    }
    
    
    NSEvent.addLocalMonitorForEvents(matching: .leftMouseUp){
        self.mouseUp(with: $0)
        return $0
    }
    
  • -->仍然得到上述错误

    问题:如何使这两种方法在我的ViewController中工作

    <> >强>重要注意事项:< /强>请考虑使用<代码> SWIFT 4.0.2和XCODE <代码> 9.1生成9B55 < /代码>

    < P>错误:

    Assertion failure in -[NSEvent touchesMatchingPhase:inView:]
    Invalid message sent to event "NSEvent: type=LMouseDown
    
    touch(匹配:in:)
    mouseDown(with:)
    调用。
    touch(matching:in:)
    的文档说明:

    此方法仅对手势事件(手势、放大、滑动、旋转等)有效

    错误:

    Assertion failure in -[NSEvent touchesMatchingPhase:inView:]
    Invalid message sent to event "NSEvent: type=LMouseDown
    
    touch(匹配:in:)
    mouseDown(with:)
    调用。
    touch(matching:in:)
    的文档说明:

    此方法仅对手势事件(手势、放大、滑动、旋转等)有效


    你会调用
    touch(匹配:in:)
    in
    mouseDown(with:)
    ?程序甚至不会跳转到mouseDown(with:),所以它不会毫无用处吗?是的,愚蠢的问题。我必须读得更好。它说的是
    MyViewController
    ,而不是
    ViewController
    。在
    MyProject
    中是否有类
    MyViewController
    ?是的,先生。这是MyProject中的自定义ViewController类。我需要ViewController类中的这些鼠标处理方法,因为我需要处理的引用都在那里…回溯说
    touch(匹配:in:)
    是从
    mouseDown(with:)
    调用的
    MyViewController
    。是否调用
    touch(匹配:in:)
    in
    mouseDown(with:)
    ?该程序甚至不会跳入鼠标向下(带:),所以它不会毫无用处吗?是的,愚蠢的问题。我必须读得更好。它说的是
    MyViewController
    ,而不是
    ViewController
    。在
    MyProject
    中是否有类
    MyViewController
    ?是的,先生。这是MyProject中的自定义ViewController类。我需要ViewController类中的这些鼠标处理方法,因为我需要处理的引用都在那里…回溯说
    touch(matching:in:)
    是从
    MyViewController的
    mouseDown(with:)
    调用的。