From ZENworks Ninja
Jump to: navigation, search

Creating a new NSDocument Instance

The problem Using MonoMac, I have a NSDocument-based application, but I'm needing to create a new NSDocument object when a button is clicked.

For example. I have in another Window I have a NSWindowController and I can do

Controller c = new Controller ();
c.Window.MakeKeyAndOrderFront (this); 

thus causing the Window to be loaded that is tied to the controller.

With the NSDocument I guess the controller is built in? So I'm expecting something like

MyNSDocument doc = new MyNSDocument ("Some Value ");
doc.Window.MakeKeyAndOrderFront (this);

Of course this doesn't work.

Additional info, for example when in the Application if you hit Command + N, then a new Document Window is loaded. This is cool and I basically need the same thing to happen, but when a button is clicked.

The Answer Using "File" / "New" or Control + N invokes newDocument: on the application's shared document controller, which is the menu.xib's First Responder.

To do the same programmatically, use NSDocumentController.SharedDocumentController to get the application's shared document controller, then invoke NewDocument () on it (you can pass null as sender):

var controller = (NSDocumentController)NSDocumentController.SharedDocumentController;
controller.NewDocument (null);