makeFirstResponder:并不总是带光标

时间:2016-03-06 16:42:22

标签: swift nstextfield first-responder becomefirstresponder

当我使用键盘快捷键显示我的应用程序或点击状态栏中的图标时,我设置了第一响应者。 makeFirstResponder:总是成功(返回true),其中包含一个非零窗口和一个非零NSTextField。然而,它并不总是采取光标' (即将闪烁移动到NSTextField)。

例如,

  • 显示应用 - 带光标✓
  • 点击外部应用
  • 再次显示应用 - 不接受光标(即使makeFirstResponder返回true)。

以下是我尝试这样做的方法:

//Find the key window. I don't think this is the problem because self.window produces the same results. 
var keyWindow:NSWindow = popover.contentViewController!.view.window!

for window in NSApplication.sharedApplication().windows{
    if (window.keyWindow){
        Swift.print("window \(window) is the key window")
        keyWindow = window
    }
}

//iFR is a variable that I use to keep track of which NSTextField I want to focus on. It's always a valid textField and most of the time (but not always) it's the only NSTextField in the view. 
if (initialFirstResponder != nil)
    {
        if keyWindow.makeFirstResponder(initialFirstResponder)
        {
            Swift.print("first responder success")
        }else
        {
            //Never happens:
            Swift.print("first responder FAIL")
        }

    }else
    {
         Swift.print("no initial firstResponder")
    }

1 个答案:

答案 0 :(得分:0)

我觉得我正在将响应者链束缚到提交中,但是,

NSApp.activateIgnoringOtherApps(true)

导致光标始终被捕获(并且很好地被问到)去我想要的地方。