Mac App Storyboard - Access Document in NSViewController

前端 未结 3 1380
旧巷少年郎
旧巷少年郎 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:43

    This does not directly address the question. But the Q&A link below shows how to access the data model in your document from NSControl objects using bindings, by utilising the preparedObject of the NSViewController.

    https://developer.apple.com/library/content/qa/qa1871/_index.html

    I set the representedObject for the NSViewController in Document.m as following:

    - (void)makeWindowControllers {
        NSWindowController* wc = [[NSStoryboard storyboardWithName:@"Main" bundle:nil] instantiateControllerWithIdentifier:@"Document Window Controller"];
        NSViewController* vc = wc.contentViewController;
        vc.representedObject = self.model;
    
        [self addWindowController:wc];
    }
    

    Now representedObject of my ViewController is set to model. Assuming my model has a text property, I can bind any NSControl to that property through ViewController with keyPath: self.representedObject.text

提交回复
热议问题