0

I have made a pretty simple mac app with two windows. I am an iOS developer so I am very familiar with swift but a few of the UI elements in iOS apps don’t translate well when building OS X apps. I want to take advantage of the menu bar at the top of the screen. I want to click “New” under file which is there by default and open a new window of my initial VC. How do I do that?

I have read a bunch of posts and they have told me to build a new menu bar but I feel like there should be an easier way with the menu bar that is there by default. How do I call a specific VC even once I have managed to create an outlet or add an action for the new button? Can I just instantiate the VC like we would in iOS? I just want the easiest way to do this.

This is what I used to present the new window:

let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil)
let myWindowController = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "LoginVC")) as! LoginViewController

if let window = NSApplication.shared.mainWindow 
    window.contentViewController = myWindowController //as! NSWindowController  // just swap
}
Nevin Jethmalani
  • 2,726
  • 4
  • 23
  • 58
  • Simply making your application document-based should suffice for most purposes, since creating a new instance of a view controller from File->New is usually something you do when creating a new document. – Charles Srstka Nov 01 '17 at 21:52
  • How do I do that? – Nevin Jethmalani Nov 01 '17 at 21:53
  • If you check the "Create Document-Based Application" check box when making the project, Xcode will set it up from a template and it'll already be ready to go, with the menu items all linked up already and everything. All you'll have to do will be to supply the implementation of the view controller. – Charles Srstka Nov 01 '17 at 23:05

1 Answers1

0

By default the "New" menu item will send the newDocument(_:) message to the "first responder". If any object along the responder chain implements it, then the menu item will be enabled, otherwise the menu item is disabled.

To respond to this message you could either:

  • implement func newDocument(_ sender: Any?) somewhere in the responder chain (probably somewhere towards the end of it, like in the app delegate). In a document-based application the NSDocumentController would already handle this.
  • set your own action and target for the "New" menu item to call another method on a specific object to have it create and display the new window. This would not work for actions that are contextual based on where in the application the user is (for example Copy or Paste).
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • it looks like the newDocument function worked! But how do I bring up a VC now? Or a new window? What is the best way to do that? – Nevin Jethmalani Nov 01 '17 at 22:00
  • because I am using swift 4, I just had to add `@objc` before it – Nevin Jethmalani Nov 01 '17 at 22:00
  • See [How to open a new window on button click in Cocoa Mac Application?](https://stackoverflow.com/q/5547741/608157). – David Rönnqvist Nov 01 '17 at 22:03
  • this is being done from the app delegate so it is a little different from the question you referenced. When I try this `var wc = NSWindowController(windowNibName: NSNib.Name(rawValue: "MainWindow"))` `wc.showWindow(self)` It does not work. It says -[NSWindowController loadWindow]: failed to load window nib file 'MainWindow'. – Nevin Jethmalani Nov 01 '17 at 22:16
  • I have set the Storyboard ID to "MainWindow" for the window. – Nevin Jethmalani Nov 01 '17 at 22:17
  • Scroll down and you'll see that one of the answers show how to load a window controller from a storyboard. – David Rönnqvist Nov 01 '17 at 22:24