Reputation: 213
I like to build contextual menus in the storyboard, as opposed to doing it programmatically.
Typically, in an NSOutlineView
, I want to display a particular context menu based on the current selection.
Rather than building a global menu and hiding/showing certain menu items, is there a way to switch menus dynamically by simply assigning the relevant @IBOutlet
property?
class ViewController: NSViewController {
@IBOutlet var menuOne: NSMenu!
@IBOutlet var menuTwo: NSMenu!
@IBOutlet var menuThree: NSMenu!
// ...
}
extension ViewController: NSMenuDelegate {
func menuNeedsUpdate(_ menu: NSMenu) {
if // ... {
menu = menuOne // Error: Cannot assign to value: 'menu' is a 'let' constant
} else if // ... {
menu = menuTwo // Error: Cannot assign to value: 'menu' is a 'let' constant
} else if // ... {
menu = menuThree // Error: Cannot assign to value: 'menu' is a 'let' constant
}
}
}
Apparently, another option is to subclass NSOutlineView
and override the menu(for:) method
, but I’m unsure how to access my @IBOutlet
properties in that case:
class MyOutlineView: NSOutlineView {
override func menu(for event: NSEvent) -> NSMenu? {
// ...
// How am I supposed to access my @IBOutlet properties?
super.menu(for: event)
}
}
Upvotes: 1
Views: 21