我只用源代码编写了 NSTextField 。但 NSTextField 不可点击,键盘事件也不起作用。
open class SearchBarView: ViewWrapper, NSTextFieldDelegate {
var textField: NSTextField!
---- TextField init
textField = NSTextField(frame: NSMakeRect(0, 0, 200, 60))
textField.isEditable = true
textField.font = NSFont.systemFont(ofSize: CGFloat(28.0))
textField.textColor = NSColor.gray
textField.isSelectable = true
textField.delegate = self
self.addSubview(self.textField)
---- func
open override func controlTextDidChange(_ notification: Notification)
{
Swift.print("asdasd")
let object = notification.object as! NSTextField
self.textField.stringValue = object.stringValue
}
如何让键盘修复它?
最佳答案
为了使 NSTextfield 正常工作,如果您使用自定义窗口,请检查您的窗口是否是 keyWindow() 和 mainWindow() 是。
- (BOOL)canBecomeKeyWindow {
return YES;
}
- (BOOL)canBecomeMainWindow {
return YES;
}
如果您有自定义窗口,请尝试使用您的 swift 代码。
关于swift - 在cocoa中,NSTextField没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43906299/