【问题标题】:Cocoa UI IBAction call seems to corrupt NSTimerCocoa UI IBAction 调用似乎破坏了 NSTimer
【发布时间】:2018-02-19 22:20:22
【问题描述】:

我有一个函数:

NSTimer *learnUSBTimer=nil; - (void)showUSBWindow:(id)sender { [self updateDeviceButton:@"Cancel"]; [self hideDeviceClearButton:TRUE]; [selectedDeviceField setStringValue:@"Insert the USB device..."]; learnUSBMode=1; if(learnUSBTimer) { if([learnUSBTimer isValid])[learnUSBTimer invalidate]; learnUSBTimer=nil; } }

看来我发送到计时器对象的消息正在发送到 UI 对象:

2018-02-19 14:02:34.649581-0500 TokenLock[4068:819373] -[NSBox isValid]: unrecognized selector sent to instance 0x60000016bc40 2018-02-19 14:02:34.649691-0500 TokenLock[4068:819373] [General] -[NSBox isValid]: unrecognized selector sent to instance 0x60000016bc40 2018-02-19 14:02:34.651799-0500 TokenLock[4068:819373] [General] ( 0 CoreFoundation 0x00007fff4f33833b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x00007fff7607c942 objc_exception_throw + 48 2 CoreFoundation 0x00007fff4f3cf5e4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 3 CoreFoundation 0x00007fff4f2aff03 ___forwarding___ + 1443 4 CoreFoundation 0x00007fff4f2af8d8 _CF_forwarding_prep_0 + 120 5 TokenLock 0x0000000100014cdc -[AppController showUSBWindow:] + 252 6 AppKit 0x00007fff4d02375a -[NSApplication(NSResponder) sendAction:to:from:] + 312 7 AppKit 0x00007fff4cac9933 -[NSControl sendAction:to:] + 86 8 AppKit 0x00007fff4cac985b __26-[NSCell _sendActionFrom:]_block_invoke + 136 9 AppKit 0x00007fff4cac9761 -[NSCell _sendActionFrom:] + 183 10 AppKit 0x00007fff4cb0aa18 -[NSButtonCell _sendActionFrom:] + 97 11 AppKit 0x00007fff4cac7fca -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2438 12 AppKit 0x00007fff4cb0a75f -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 777 13 AppKit 0x00007fff4cac6a64 -[NSControl mouseDown:] + 965 14 AppKit 0x00007fff4d1c2959 -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 5891 15 AppKit 0x00007fff4d1bf5b0 -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 2359 16 AppKit 0x00007fff4d1be85c -[NSWindow(NSEventRouting) sendEvent:] + 497 17 AppKit 0x00007fff4d01f617 -[NSApplication(NSEvent) sendEvent:] + 307 18 AppKit 0x00007fff4c880d9d -[NSApplication run] + 812 19 AppKit 0x00007fff4c84ff1a NSApplicationMain + 804 20 TokenLock 0x0000000100001ea1 main + 97 21 TokenLock 0x0000000100001e34 start + 52 )

我不明白我的 NSTimer 是如何被分配给被单击的 UI 框以执行该功能的?

【问题讨论】:

  • 您的错误是告诉您learnUSBTimerNSBox,而不是NSTimer 告诉我们您设置此变量的位置。问题不在于此处的代码,而在于您设置此变量的其他地方。
  • 当您收到“无法识别的选择器发送到实例”错误时,第 1 步始终在启用僵尸的情况下运行。你试过吗? (如果您不知道如何在启用僵尸的情况下运行,只需使用您最喜欢的搜索引擎,您就会找到分步说明。)

标签: macos user-interface cocoa ibaction


【解决方案1】:

您的代码中似乎存在一些内存管理问题。并且对象未正确分配/取消分配或未正确初始化取消初始化。 因此,要解决这个问题,只需通过在项目目标设置中启用僵尸来运行您的代码。

【讨论】:

    【解决方案2】:

    事实证明这个计时器触发了一次。我没有在该代码中将它设置为 nil,因此它本质上变成了一个孤儿,因为系统在之后释放了实际的计时器对象。

    在触发时将其设置为 nil 可解决此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-25
      • 2012-12-19
      • 2020-02-22
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多