簡體   English   中英

NSTextField 焦點

[英]NSTextField Focus

如果我不關注 textField,如何移除對 NSTextField 的關注?

我有一個 NSTextField,我設置了 Action: Sent On End Editing。 單擊 textField 后,當我單擊視圖上的其他位置時,FOCUS 將始終位於我的 NSTextField 中。 此外,我使用makeFirstResponderresignFirstResponder但都是徒勞的。 任何的想法?

@IBAction func endOfEditing(sender: NSTextField) {
    view.window?.makeFirstResponder(nil)
    view.window?.resignFirstResponder()
}

例如,無論我按下哪里,FOCUS 都將始終位於 textField 中。 (即使我按下了按鈕)

在此處輸入圖片說明

當我在 textField 之外單擊時如何移除焦點?

在此處輸入圖片說明

試試這個:改變“view.window?” 到“發件人”

@IBAction func endOfEditing(sender: NSTextField) {
   sender.makeFirstResponder(nil)
   sender.resignFirstResponder()
}

斯威夫特 4

對於那些仍然在關注焦點和未聚焦的人:我不知道 2016 年是否是這種情況,但在 Swift 4 中,我的NSTextField失去焦點,除非我單擊另一個“可選”出口。

例如,如果我在NSTextField之后點擊一個NSButton ,它就不會失去焦點。 我懷疑NSButton捕獲了該事件,從而阻止文本字段知道它必須失去焦點。

這是一個非常激進的解決方案,它肯定會使NSTextField散焦

@IBAction func clickMyButton(_ sender: NSButton) { // or anywhere else

    self.quantityTextField.isEnabled = false
    self.quantityTextField.isEnabled = true

}

不是很性感,但它是可靠的

編輯:剛看到它,它解決了@Lee 的問題

嘗試將此功能添加到您的 viewController

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    // I assume your textField call myText
    myText. resignFirstResponder()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM