清除事件我有一個視圖控制器與鏈接到一個IBAction爲一個NSButton假設perform()
。該按鈕與返回鍵相同。 不過返回鍵也被用在了其他臨時措施。在沒有涉及到以前的視圖控制器的其他文件,我有一個的NSTextField這是可編輯的,所以返回鍵將驗證文本更改。如何在斯威夫特
我想驗證我的文字兵不血刃perform()
從功能等效鍵按鈕調用。
目前我發現的唯一解決方案是發送通知textIsBeingEditing
當我的文本字段成爲FirstResponder和其他代理函數controlTextDidEndEditing
時。
這是我通知的選擇:
@objc func trackNameIsBeingEditedNotification(_ notification: Notification) {
guard let value = notification.userInfo?["trackNameIsBeingEdited"]
as? Bool else {
return
}
if value {
backButton.keyEquivalent = ""
backButton.action = nil
} else {
myButton.keyEquivalent = "\r"
myButton.action = #selector(self.perform(_:))
// Here the `perform()` function is fired but I would avoid this behaviour…
}
}
沒有任何辦法取消鍵事件,以防止perform()
被解僱就在我設置myButton.action = #selector(self.perform(_:))
? 我看到一個函數flushBufferedKeyEvents()
,但我完全不知道如何使用它
繼續[在Swift中取消鍵盤事件](https://stackoverflow.com/questions/46401695/cancel-key-event-in-swift?noredirect=1#comment79769275_46401695) – Willeke