ixany
ixany

Reputation: 6070

Popup NSMenu on right-click on NSView (Swift)

I subclassed a NSView to handle mouseDown/rightMouseDown events. Now i want to popUp a contextual NSMenu (which is a IBOutlet) by right-clicking.

NSMenu.popUpContextMenu(statusMenu, withEvent: theEvent, forView: self)

... but it says "statusMenu is not a member of AppDelegate". How can i access vars outside the class?

EDIT:

The IBOutlet ist located in the AppDelegate:

@IBOutlet weak var statusMenu: NSMenu!

I try to call it from my custom NSView:

class customView : NSView { 
  override func rightMouseDown(theEvent : NSEvent) {
    // NSMenu.popUpContextMenu ???
  }
}

Upvotes: 0

Views: 1707

Answers (1)

Grimxn
Grimxn

Reputation: 22507

There are various ways you could do this:

1) Create an IBOutlet in your NSView and connect the NSMenu to it.

2) Create an IBOutlet in your NSView, connect your AppDelegate to it, and refer to the menu through that outlet (if you didn't want for some reason to have two references to the menu)

3) Get a runtime reference to the AppDelegate in the NSView - the syntax is

var appDelegate = NSApplication.sharedApplication().delegate as AppDelegate

and refer to the menu through that.

Note that in case 3 "AppDelegate" is whatever name your AppDelegate class actually is (usually, but not necessarily, "AppDelegate"). The same is true for the type of the IBOutlet in case 2.

Upvotes: 1

Related Questions