I have set an NSMenuItem's view to a custom view, which contains a textfield. When I run the program, everything works as expected. NSTextField is part of the NSStatusItem and accepts focus and I can type things in it.
After closing the menu and going to a different window, When I click the menu and try to bring the focus back to the NSTextField, it becomes readonly.
Code:
import Cocoa
import CoreLocation
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSTextFieldDelegate {
@IBOutlet weak var window: NSWindow!
var statusItem: NSStatusItem
@IBOutlet weak var statusMenu: NSMenu!
@IBOutlet weak var firstMenuItem: NSMenuItem!
@IBOutlet weak var textField: NSTextField!
override func awakeFromNib() {
statusItem.menu = statusMenu
}
override init() {
statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength)
super.init()
}
func applicationDidFinishLaunching(aNotification: NSNotification) {
if let button = statusItem.button {
button.image = NSImage(named:"StatusBarButtonImage")
}
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
Is there a way to bring the focus back to NSTextField?
I tried becomeFirstResponder, canBecomeKeyView but they do not seem to be working in this case.