IliasMay IliasMay - 3 months ago 31
Swift Question

NSTextField or NSSecureTextField in NSMenu() as NSMenuItem()

I'm currently working on a menubar app and I'm trying to implement a NSTextField or NSSecureTextField.
This is what I do to add NSMenuItems

let menu = NSMenu()
menu.addItem(NSMenuItem(title: "Save Test Data", action: #selector(AppDelegate.saveData), keyEquivalent: ""))


but trying to add a NSTextField like this:

menu.addItem(NSTextField)


brings this Error:

Cannot convert value of type '(NSTextField).Type'(aka 'NSTextField.Type') to expected argument type 'NSMenuItem'


But how should I implement a NSTextField using NSMenuItem?

Answer

You cannot add other elements as NSMenuItem to a menu directly.

To implement a text field you have wrap it in a custom NSView and assign the view to the view property of the menu item.