3

I'm working on a Swift 4 Cocoa Mac OS X (not iOS) project with XCode 9. I have two NSWindows in my main storyboard with different (subclasses of) NSViews. Currently only one window holds the storyboard entry point and so the other one does not appear when the application begins, and I want both windows to appear when the application is loaded.

I've tried googiling with different keywords but so far couldn't find a way. The only method I found was to connect a segue from a button or menu in one window to the other window, so that the other window appear whenever the button is pressed. Is there any of making both windows appear in the beginning the 'right way' (preferablly using functionalities of XCode storyboard)?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jineon Baek
  • 133
  • 3
  • 1
    Have you tried the [tutorial](https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/index.html#//apple_ref/doc/uid/TP40015214-CH2-SW1)? Windows are not really elements. The tutorial will walk you through a basic use of views that may help you write down your real requirements. – Efren Nov 08 '18 at 02:15
  • Thanks for your input. Now I see that I need to study the precise structures of an application (what exactly does a class do, how classes interact with each other, etc). – Jineon Baek Nov 08 '18 at 02:56
  • @Efren Will it be OK to go through tutorials/resources for iOS to understand Mac OS Cocoa programming? When I started learning I skipped the tutorial you mentioned because it was for iOS, but now I have the impression that they are quite similar. – Jineon Baek Nov 08 '18 at 02:58
  • @Efren And, if you don't mind, can you recommend some resources for understanding the structure of a Cocoa application precisely for a beginner? I will do my search, but the resources I have seen on the web seems to either does not describe the inner mechanics well or too technical to digest (assuming prior understanding of Cocoa classes). – Jineon Baek Nov 08 '18 at 03:01
  • @Efren Sorry if I asked too many questions and please don't feel obligated to answer - maybe I can make other questions in stackoverflow. Thanks again for the link! – Jineon Baek Nov 08 '18 at 03:06
  • 1
    Yeah Apple [docs](https://developer.apple.com/library/archive/documentation/General/Conceptual/MOSXAppProgrammingGuide/Introduction/Introduction.html) are sadly mostly not updated to swift. This [list](https://www.creativebloq.com/app-design/how-build-app-tutorials-12121473/4) may help, in particular [item 5](https://www.raywenderlich.com/704-macos-view-controllers-tutorial). That last website in particular helped me a lot with iOS gaps in doc, even though some pages may be with previous swift versions. – Efren Nov 08 '18 at 03:35

1 Answers1

3

You have to get a reference to the window controller in your storyboard from the second window you want to show. Add this code to your NSApplicationDelegate in applicationDidFinishLaunching. Don't forget to set the identifier from the WindowController in the Storyboard

let storyboard = NSStoryboard(name: "Main", bundle: nil)
let windowController = storyboard.instantiateController(withIdentifier: "MyWindowController") as! NSWindowController
windowController.showWindow(self)
Sulthan
  • 128,090
  • 22
  • 218
  • 270
Marc T.
  • 5,090
  • 1
  • 23
  • 40
  • It's working like a charm. Will go through the documentation for what's happening behind. Thanks! – Jineon Baek Nov 08 '18 at 10:54
  • I think this is worth adding: since we are adding a controller from the main storyboard, we don't need to instantiate another storyboard named "main" and just go for `NSStoryboard.main!.instantiateController` call. – Jineon Baek Nov 08 '18 at 16:29
  • But now I question if it is good for two appearing windows to be in the same 'main' storyboard. Whether this is good or not will depend on each situation. If using other storyboard we should manually instantiate a `NSStoryboard` object. – Jineon Baek Nov 08 '18 at 16:31
  • 1
    For your first question of course you can do it with main as well but I wanted to outline how we could refer to any storyboard in the project. It depends on you and if you want all your window in one or multiple Storyboards. I am using normally different Storyboard for each window. (E.g. Preferences, About panel) At the end they will end up in separat xib files no matter how you organize it. – Marc T. Nov 08 '18 at 18:37