macOS: NSMenuItem Print

Hi all,

I'm sorry if this question has already been made.


I'm writing a macOS app with Swift. This App is StoryBoard based and in the main Window there is an NSView where I do some graphics.

Well, in the NSViewController associated I have implemented (overriden) the


@IBAction func saveDocument(_ sender: Any?)

@IBAction func openDocument(_ sender: Any?)


in order to save and open data associated with my NSView (accessible from the NSViewController using an Outlet).


In order to call the "print" action of my NSView object, I need to connect in some way the "Print" menu item to an action. I though it would have been enough as for the "save" and "open" to override the


@IBAction func printDocument(_ sender: Any?)


but nothing happes: when I push CMD-P or I select "Print" from the Main Menu, is always called the standard Print Dialog (I suppose the one definited for the First Responder).


If I try to connect the NSMenuItem "Print" to a normal IBAction, the Action is not discovered by XCode but the funny thing is, if in my saveDocument or openDocument I invoke the print action of my NSView, the Print Panel is opened properly and I can print my content!!


Can you please help me to achieve my goal of printing my NSView?


Thanks in advance!!!

Accepted Reply

OK, it was really easy: I just needed to create an action in the First Responder and connect the NSMenuItem to the new Action.

Then once implemented a function in the NSViewController with the same name of the action created in the First Responder, my function has been called :-)

Replies

Did you disconnect the previous NSMenuItem "Print" action ?

Hi Claude,

thanks for your reply.


Yes, if I disconnect from the firstresponder.print the only effect I have is that the menu item is no more available (grayed). It cannot still be connected to any IBAction :-(

OK, it was really easy: I just needed to create an action in the First Responder and connect the NSMenuItem to the new Action.

Then once implemented a function in the NSViewController with the same name of the action created in the First Responder, my function has been called :-)