How do you bind a storyboard view to a Core Data entity when using NSDocument?

后端 未结 1 1685
一向
一向 2021-02-10 04:49

I\'m building an OS X app that uses core data, NSDocument, storyboards, and Cocoa bindings.

My expectation is that the following occurs:

  1. An instance of

相关标签:
1条回答
  • 2021-02-10 05:33

    Steps to create a sample Xcode Document-Based Application project with Core Data, Storyboard, NSArrayController, NSTableView and Bindings.

    Step 1 Create a Xcode project. Choose OS X Cocoa Application and select ‘Use Storyboards’, ‘Create Document-Based Application’ and ‘Use Core Data’.

    Step 2 Select the data model. Add entity ‘Person’ and string attributes ‘name’ and ‘address’.

    Step 3 Select Main.storyboard. Add a NSArrayController to the view controller scene. Set Mode to ‘Entity Name’ and set Entity Name to ‘Person’. Check ‘Prepares Content’. Bind Managed Object Context of the array controller to View Controller, Model Key Path representedObject.managedObjectContext.

    Step 4 Go to the view of the view controller scene. Remove ‘Your document contents here’. Add a NSTableView. Bind Content to Array Controller, Controller Key arrangedObjects. Bind Selection Indexes to Array Controller, Controller Key selectionIndexes. Bind Sort Descriptors to Array Controller, Controller Key sortDescriptors.

    Step 5 Bind Value of the text fields in the table view to Table Cell View, Model Key Path objectValue.name and objectValue.address. Check 'Conditionally Sets Editable'.

    Step 6 Add two Push Buttons ‘Add’ and ‘Remove’ to the view of the view controller scene. Connect the actions to actions add: and remove: of the array controller.

    Step 7 (Objective-C) Select Document.h. In method makeWindowControllers, replace statement [self addWindowController:… by

    NSWindowController *aWindowController = [[NSStoryboard storyboardWithName:@"Main" bundle:nil] instantiateControllerWithIdentifier:@"Document Window Controller"];
    [self addWindowController:aWindowController];
    aWindowController.contentViewController.representedObject = aWindowController.document;
    

    Step 7 (Swift) Select Document.swift. In method makeWindowControllers, at the end after self.addWindowController(windowController) add

     windowController.contentViewController!.representedObject = windowController.document
    

    Step 8 Build, Run, Test.

    0 讨论(0)
提交回复
热议问题