Reputation: 1667
I am trying to use the Core Data Entity in my app by dragging it onto a nib file within Interface Builder but it doesn't appear to be in my Library of controls.
Any idea how to get it in there?
Thanks
Graeme
Upvotes: 0
Views: 1779
Reputation: 48649
The Core Data Entity doesn't exist in the Cocoa Library in Xcode 6.2. Instead, you can use an ArrayController
.
1) Create a New project in Xcode: enter the project name and check the box that says Core Data
.
2) In your project files, select the .xcdatamodeld file, and in interface builder setup an entity
, which is like a Cocoa class with some @properties:
Click on Add Entity
at the bottom of the window. In the image above, the entity is named Book. That line initially will say Entity
instead of Book
. Double click on Entity
and type in Book
.
Click the +
in the attributes area to add the attributes and their types. To the far right, (with the rightmost icon selected at the top) you can check or uncheck other properties for the attribute, for instance uncheck optional.
As far as I can tell, setting up an entity is just like creating an Objective-C class with a bunch of @properties.
3) Select MainMenu.xib and drag a TableView(make it cell based) and a couple of square buttons onto the window:
For the buttons, in the Attributes Inspector next to Image, select NSAddTemplate and NSRemoveTemplate respectively. In the TableView, double click on the column headers and enter the column names.
Once you have your view set up, drag an ArrayController from the Library to the Dock. In the Identity Inspector, change the ArrayController's Label
to BookController as shown in the image above. The label allows you to refer to that specific ArrayController, which is handy if you have multiple ArrayController’s in your xib file.
4) Next, in the Attributes Inspector specify the entity, or the type of the object, that the ArrayController will store and manage:
Also check Prepares Content
. That will cause the data saved to disc to load in the TableView when the TableView launches.
5) With the ArrayController still selected, go to the Bindings Inspector, and under Parameters
bind the ArrayController to AppDelegate
and for the keypath enter managedObjectContext
. The managedObjectContext is responsible for writing the data to disc:
6) Next, bind the columns of the TableView to the ArrayController. Make sure you are selecting the Book Title
column:
In the Bindings Inspector under the Value binding, bind to the BookController
(the label given to the ArrayController). The Controller key arrangedObjects
is a sorted array of all the objects in the ArrayController. And by specifying the Model keypath as title
, you are telling the column to display the title of every object in arrangedObjects.
Select the Author column in the NSTableView and setup the binding in a similar fashion.
7) Select the +
square button, and Ctrl+drag from the button to the ArrayController. After releasing, choose add:
from the popup menu. Then select the -
square button, and Ctrl+drag from the button to the ArrayController. After releasing, chose remove:
from the popup menu.
Finally, setup a binding for the -
square button, which will disable the button when there are no items in the TableView to remove:
Run the application and use the +
button to insert entries into the TableView. I notice that if I stop the application using Xcode's Stop
button, the data entered into the TableView does not save to disc. Instead, I have to choose Quit under the application name in the menu bar in order to save the data to disc.
Upvotes: 2
Reputation: 7327
I notice this is iOS tagged, so bad news: Core Data support in Interface Builder only exists for Macs, not iDevices (so far.) Make a MacOS X nib, not an ios nib, and you'll see "Managed Object Context" and "Core Data Entity" in the library. IB's Core Data support heavily uses Cocoa Bindings, which are also only available in MacOS proper (so far); without bindings it's not really meaningful to wire up model objects in the nib.
To see how to use core data without setting things up in IB, make a new iOS project with the "Navigation-Based Application" template, and check the "use core data for storage" checkbox. The meat is in the app delegate and RootViewController classes it creates; the nibs just contain VCs and UIViews, as normal.
Upvotes: 2