How does OS X load a storyboard based app, and how does it do window management?
Asked Answered
B

1

9

I'm working on a brand new OS X app, and I've taken the daring route of working with a few technologies I haven't used much before. (I'm an iOS developer.)

I'm using Storyboards, Swift, and Core Data in my app, all from the Xcode template. When my app starts, it creates an NSWindowController from a Storyboard.

In another OS X app I made in Objective-C, I use the delegate method applicationShouldHandleReopen:hasVisibleWindows: to tell the window controller to bring up the window controller's window.

I know that the language semantics remain the same in Swift, but Core Data and Storyboards leave me with two questions.

  1. Does the fact that my project uses Core Data change it into a "document-based app" and therefore change the way windows are managed?

  2. In my old project I'm using nibs instead of storyboards. It seems that I'm manually instantiating an NSWindowController there, and using it to manage re-opening. If there's a way to get at the one my Storyboard is undoubtedly making for me, that would be optimal, right? Is just attaching an outlet to my App Delegate the way to go? Is there another convention?

Baisden answered 3/12, 2014 at 2:8 Comment(6)
Related: #3683849Baisden
You've bitten off too much at once.Fanaticism
Document architecture is quite different and adds more to the hierarchy. Each NSDocument instance can have multiple window controllers. I'd recommend tackling storyboards, core data and document arch separately first.Fanaticism
I've worked with Core Data and Storyboards before, but never in this context. (I did not choose Document Based, so I don't think that's the issue.)Baisden
Core data definitely does not automatically make a document based app. You have to select that in addition. It just gives you an object store same as ios.Fanaticism
Keep in mind iOS apps are conceptually single window full screen apps. On os x the window is the root of the storyboard and you just manage view segues there in the window and it will make sense. There are a lot less segues because apps tend to be not full screen and tend to have multiple windows.Fanaticism
A
6
  1. No. Checking the "Create Document-Based Application" is what made it a document based app. The decision to use Core Data is separate.

  2. The Storyboard you get when you make an OS X Document based app is a bit atypical. Notice that there is no arrow in the storyboard telling you where the entry point it. What happens is "Document" is instantiated. It has a method:

    override func makeWindowControllers() {
        // Returns the Storyboard that contains your Document window.
        let storyboard = NSStoryboard(name: "Main", bundle: nil)!
        let windowController = storyboard.instantiateControllerWithIdentifier("Document Window Controller") as NSWindowController
        self.addWindowController(windowController)
    }
    

It goes looking in the storyboard for a controller with the identifier @"Document Window Controller" (which just so happens is that window controller). Document keeps a collection of windowControllers. so Document instantiates it and adds the windowController to that list.

So Document already has a reference to the window controller in Document.windowControllers

(Can someone tell me WHY the Document gets created though? I can't see what actually triggers its creation)

Aubry answered 22/12, 2014 at 6:38 Comment(2)
It looks like you won't get a window by default till the user creates a new document by clicking the dock icon. It's probably for the better though, since you can show something similar to a list of files in icloud/hd like TextEdit or Pages does insteadOfeliaofella
There must be an automatic "newDocument:" message that gets sent when you open an app. That makes sense.Aubry

© 2022 - 2024 — McMap. All rights reserved.