IliasMay IliasMay - 1 year ago 125
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:


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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download