Hello !
Thanks for a great book, it really speeds up learning process.
Out of curiosity I’ve tried creating project for chapter 9 with Storyboards support.
To access NSDocument I’m using this peace of code :
[code]
class ViewController: NSViewController {
weak var document : Document!
…
override func viewWillAppear() {
if let doc = self.view.window?.windowController()?.document {
document = doc
}
assert(document != nil, “document property is not set !”)
}
…
}[/code]
All binding are set, it seems like everything is working. But is this the best way of doing things when using Storyboards ?
I think I could use representedObject, but I feel like it can cause troubles under certain circumstances.
Unfortunately, accessing a view controller’s document is quite sticky. Your approach is reasonable. Another approach is to use a computed property:
var document: Document {
if let document = self.view.window?.windowController()?.document {
return document
}
fatalError("Document not found.")
}
Thanks for advice !
I’ve ended up with overriding document property of NSWindowController
[code]class MainWindowController: NSWindowController {
// MARK: Overrides
override var document: AnyObject? {
didSet{
super.document = document
if let viewController = window?.contentViewController as? ViewController {
viewController.document = document as? Document
}
}
}
}[/code]
So it’s get assigned when NSDocument calls makeWindowControllers() function