Mac App Storyboard - Access Document in NSViewController

前端 未结 3 1382
旧巷少年郎
旧巷少年郎 2021-02-12 12:08

I am currently fighting with NSDocument in a document based Storyboard based Cocoa Application (Objective C). Can anybody tell me how i can access the document in the NSViewCon

3条回答
  •  忘掉有多难
    2021-02-12 12:36

    I was just wrestling with this myself. I started with the standard Yosemite template and was trying to use [self.view.window.windowController document] in -viewDidLoad. At that point, self.view.window is nil, so there's no way to get to the document.

    The trick is to wait until -viewWillAppear. By the time it is called, self.view.window is populated and the document is available.

    Sequence: -makeWindowControllers invokes -self addWindowController: with the storyboard's -instantiateControllerWithIdentifier: result. -addWindowController: triggers a call to the VC's -viewDidLoad before returning. Then, finally, -viewWillAppear is called (and the document is available).

提交回复
热议问题