Aug 7, 2017 - Creating a Document-Based Mac Application Using Swift and Storyboards. Application checkbox and deselect the Use Core Data checkbox. My initial attempt at writing the code for this tutorial did set the text view's. Data input devices Data storage Networking Print & Scan Projectors Smart wearables Software Telecom & navigation TVs & monitors Warranty & support. Core Data Programming Guide.
This is an abridged chapter from our book, which has been completely updated for Swift 4.2 and iOS 12. This tutorial is presented as part of our — enjoy! Welcome to Core Data! In this tutorial, you’ll write your very first Core Data app. You’ll see how easy it is to get started with all the resources provided in Xcode, from starter code templates to the Data Model editor. You’re going to hit the ground running right from the start. By the end of the tutorial you’ll know how to:.
Model data using Xcode’s model editor. Add new records to Core Data. Fetch a set of records from Core Data. Display the fetched records using a table view. You’ll also get a sense of what Core Data is doing behind the scenes, and how you can interact with the various moving pieces. Getting Started Open Xcode and create a new iOS project based on the Single View App template.
Name the app HitList and make sure Use Core Data is checked. Checking the Use Core Data box will cause Xcode to generate boilerplate code for what’s known as an NSPersistentContainer in AppDelegate.swift.
The NSPersistentContainer consists of a set of objects that facilitate saving and retrieving information from Core Data. Inside this container is an object to manage the Core Data state as a whole, an object representing the Data Model, and so on. The standard stack works well for most apps, but depending on your your app and its data requirements, you can customize the stack to be more efficient.
Note: You’ll come across the word managed quite a bit when dealing with Core Data. If you see “managed” in the name of a class, such as in NSManagedObjectContext, chances are you are dealing with a Core Data class.
“Managed” refers to Core Data’s management of the life cycle of Core Data objects. However, don’t assume all Core Data classes contain the word “managed”. For a comprehensive list of Core Data classes, check out the Core Data framework reference in the documentation browser. Since you’ve elected to use Core Data, Xcode automatically created a Data Model file for you and named it HitList.xcdatamodeld. Open HitList.xcdatamodeld.
As you can see, Xcode has a powerful Data Model editor: The Data Model editor has a lot of features you can explore later. For now, let’s focus on creating a single Core Data entity. Click on Add Entity on the lower-left to create a new entity. Double-click the new entity and change its name to Person, like so: You may be wondering why the model editor uses the term Entity. Weren’t you simply defining a new class?
As you’ll see shortly, Core Data comes with its own vocabulary. Here’s a quick rundown of some terms you’ll commonly encounter:. An entity is a class definition in Core Data.
The classic example is an Employee or a Company. In a relational database, an entity corresponds to a table.
An attribute is a piece of information attached to a particular entity. For example, an Employee entity could have attributes for the employee’s name, position and salary. In a database, an attribute corresponds to a particular field in a table. A relationship is a link between multiple entities. In Core Data, relationships between two entities are called to-one relationships, while those between one and many entities are called to-many relationships. For example, a Manager can have a to-many relationship with a set of employees, whereas an individual Employee will usually have a to-one relationship with his manager.
Note: Like save, fetch(:) can also throw an error so you have to use it within a do block. If an error occurred during the fetch, you can inspect the error inside the catch block and respond appropriately.
Build and run the application. Immediately, you should see the list of names you added earlier: Great! They’re back from the dead (pun intended). Add a few more names to the list and restart the app to verify saving and fetching are working. Short of deleting the app, resetting the Simulator or throwing your phone off a tall building, the names will appear in the table view no matter what.
Note: There were a few rough edges in this sample app: you had to get the managed object context from the app delegate each time, and you used KVC to access an entity’s attributes rather than a more natural object-style person.name. Key Points. Core Data provides on-disk persistence, which means your data will be accessible even after terminating your app or shutting down your device. This is different from in-memory persistence, which will only save your data as long as your app is in memory, either in the foreground or in the background. Xcode comes with a powerful Data Model editor, which you can use to create your managed object model.
A managed object model is made up of entities, attributes and relationships. An entity is a class definition in Core Data. An attribute is a piece of information attached to an entity. A relationship is a link between multiple entities. An NSManagedObject is a run-time representation of a Core Data entity. You can read and write to its attributes using Key-Value Coding. You need an NSManagedObjectContext to save or fetch(:) data to and from Core Data.
Where to Go From Here? You can download the completed project for this tutorial using the “Download materials” buttons at the top or bottom of this tutorial. In this tutorial, you’ve already experienced several fundamental Core Data concepts: Data Models, entities, attributes, managed objects, managed object contexts and fetch requests.
If you enjoyed what you learned in this tutorial, why not check out, available in our store? Here’s a taste of what’s in the book: 1. Chapter 1, Your First Core Data App: You’ll click File ▸ New Project and write a Core Data app from scratch! This chapter covers the basics of setting up your data model and then adding and fetching records. Chapter 2, NSManagedObject Subclasses: NSManagedObject is the base data storage class of your Core Data object graphs. This chapter will teach you how you customize your own managed object subclasses to store and validate data. Chapter 3, The Core Data Stack: Under the hood, Core Data is made up of many parts working together.
In this chapter, you’ll learn about how these parts fit together, and move away from the starter Xcode template to build your own customizable system. Chapter 4, Intermediate Fetching: Your apps will fetch data all the time, and Core Data offers many options for getting the data to you efficiently. This chapter covers more advanced fetch requests, predicates, sorting and asynchronous fetching. Chapter 5, NSFetchedResultsController: Table views are at the core of many iOS apps, and Apple wants to make Core Data play nicely with them! In this chapter, you’ll learn how NSFetchedResultsController can save you time and code when your table views are backed by data from Core Data. Chapter 6, Versioning and Migration: As you update and enhance your app, its data model will almost certainly need to change. In this chapter, you’ll learn how to create multiple versions of your data model and then migrate your users forward so they can keep their existing data as they upgrade.
Chapter 7, Unit Tests: Testing is an important part of the development process, and you shouldn’t leave Core Data out of that! In this chapter, you’ll learn how to set up a separate test environment for Core Data and see examples of how to test your models. Chapter 8, Measuring and Boosting Performance: No one ever complained that an app was too fast, so it’s important to be vigilant about tracking performance.
In this chapter, you’ll learn how to measure your app’s performance with various Xcode tools and then pick up some tips for dealing with slow spots in your code. Chapter 9, Multiple Managed Object Contexts: In this final chapter, you’ll expand the usual Core Data stack to include multiple managed object contexts. You’ll learn how this can improve perceived performance and help make your app architecture less monolithic and more compartmentalized. And to help sweeten the deal, the digital edition of the book is on sale for $44.99! But don’t wait — this sale price is. Speaking of sweet deals, be sure to check out the great prizes we’re giving away this year with the, including over $9,000 in giveaways!
To be eligible for for this epic iOS 12 giveaway, all you have to do is, letting us know which book or course is your favorite on this list — or which upcoming book or course you’re most excited about! We hope you enjoy this update, and stay tuned for more book releases and updates!
1 NSPersistentDocument Core Data Tutorial for Mac OS X v10.4. 10 Overview of the Tutorial The task goal of this tutorial is to create a document-based application that allows a user to display and modify information about a department, employees in the department, and managerial relationships between the employees. Each document (file) contains information about a single department and the employees associated with it. The application allows the user to save a document as a file and then reopen the file, and supports undo and redo. The final user interface may look like that shown in Figure 1-1. Figure 1-1 Final User Interface Note that the emphasis in this tutorial is on functionality.
Little attempt is made to refine the user interface as would be appropriate in a shipping application. Moreover, the problem domain is chosen for consistency with other documentation and for clarity rather than for realism. 10 11 Overview of the Tutorial Tutorial Steps Tutorial Steps This document is intended to give a high-level view of creating a functional application with little code. Although some explanation is given of what happens behind the scenes, this document does not give an in-depth analysis of the Core Data infrastructure. Create the Project and User Interface The first step illustrates the creation of a Core Data document-based project in Xcode. The major initial requirement is to create the data model, which you can use to automatically create a default user interface.
Between Core Data and Cocoa bindings, this first step actually creates a fully functional application that meets most of the task goals without the need to write any code! Adding a Department Object The first step deals only with employees. The next step is to add a Department object to the document and configure the user interface appropriately. One issue that arises here is ensuring the uniqueness of the department. You want to ensure that only one department is created per document.
Copy and Paste This part of the tutorial illustrates one approach to supporting copy and paste in a Core Data application. Customizing Property Names and Alert Panels NSDocument provides an API to allow alert panels to be customized. Since Core Data provides such a rich infrastructure for specifying constraints on data values and error checking, it is sometimes the case that the user generates more than one error in a single operation. It is often useful, therefore, to customize the error presented to the user to make it as informative as possible. Metadata Support Spotlight provides users with a means of searching for files quickly and easily. To support this, you need to associate metadata with your documents. Core Data makes it easy to do this, and to write the necessary importer.
11 12 Overview of the Tutorial NSPersistentDocument Limitations NSPersistentDocument Limitations Although this tutorial does not aim to cover all the possible features you might implement in an application, some functionality is omitted due to limitations in NSPersistentDocument itself. Because of the way Core Data operates, it is not possible to easily support autosaving in an NSPersistentDocument-based application.
Core Data cannot save to a store and maintain the same changed state in a managed object context, all while keeping an unsaved stack around as the current document. For similar reasons, NSPersistentDocument does not support Save To operations. 12 13 Creating the Project, Model, and Interface This part of the tutorial guides you through building the Department and Employees application and in the process teaches you the steps essential to building a Cocoa application using Core Data and NSPersistentDocument. Create a New Project Core Data is integrated into the Cocoa framework, so any Cocoa application can use it. The Employees and Departments application you ll build is a Core Data document-based application.
Follow these steps to create the initial project: 1. Choose New Project from the File menu. Select Core Data Document-based Application in Xcode s project Assistant window, and click the Next button (as shown in Figure 2-1). 14 Creating the Project, Model, and Interface Create the Data Model 3. Enter the project name (for example, DepartmentAndEmployees ) and a destination folder for the project. Figure 2-1 Select the Core Data Document-based Application project type Create the Data Model It s common to begin developing a Cocoa application by prototyping the application s user interface. Using Core Data, however, the first step in application development is typically to create a data model (or schema) that describes the entities that you will use in your application.
Core Data uses the model to help you build more full-featured prototypes even than is possible using Cocoa and Cocoa bindings. 15 Creating the Project, Model, and Interface Create the Data Model Xcode has a data modeling tool that you use to define the schema for your application, as shown in Figure 2-2. The tool itself is described in Xcode Tools for Core Data. If you do not know how to use the tool, consult Creating a Managed Object Model with Xcode, which describes how to create a data model.
Figure 2-2 Data modeling tool After creating the project, open MyDocument.xcdatamodel in the modeling tool by selecting its icon in the project Models folder. Use the tool to define two entities, Employee and Department as specified in Table 2-1 through Table 2-4. The diagram for the finished model should look like that shown in Figure 2-3. Items of note and not covered in the tables: The class for both entities is NSManagedObject; neither entity is abstract. None of the properties is transient. Employee has a reciprocal relationship a relationship to itself that defines the manager-reports relationship.
For this part of the tutorial, the Employee s department relationship is optional. The delete rule for all relationships is Nullify. 16 Creating the Project, Model, and Interface Create the Data Model The minimum value for the Employee salary attribute is 0. 18 Creating the Project, Model, and Interface Create the User Interface 5. Interface Builder automatically creates a user interface, as illustrated in Figure 2-4. The interface contains a table view, search field, text fields for individual attributes, pop-up menu for Department and Manager, and buttons to add, remove, and fetch employees.
Array controllers are also added to the nib file to manage two arrays of employees and an array of departments. Figure 2-4 Default automatic user interface 6. Interface Builder actually does more than is required for this tutorial. Delete the Department popup menu and the Departments array controller; also delete the Department and Manager columns in the table view.
Rearrange the user interface if you wish, to make it neater. Save the nib file. You should take some time to investigate both the connections in the nib file and the behavior of the application.
The user interface is created almost entirely using bindings. In particular you should note: 18 19 Creating the Project, Model, and Interface Set the File Extension and Type The array controllers managedobjectcontext binding is bound to the File s Owner s managedobjectcontext (see the Bindings pane of the NSArrayController Inspector). The array controllers are set to manage an entity, not a class (see the Attributes pane of the Inspector). The search field has been populated with a number of predicates one for each attribute, and an additional one to search all attributes. There are two array controllers to manage collections of Employees one for the table view, and one for the pop-up menu. Set the File Extension and Type Core Data supports several types of data store XML, SQLite, and binary.
Each has their own advantages and disadvantages. XML, for example, is (to an extent) human-readable, which may be particularly useful during the early stages of development when you want to ensure that data you expected to be saved to a file has in fact been stored. The XML store, however, is verbose and comparatively slow. The SQLite store is smaller and more efficient. In particular, it has the unique advantage that its entire contents do not have to be read into memory when it is opened.
By default, the project is configured with three document types, one for each store type. You can change these directly in Xcode simply by using the Properties pane in the Target Info window as illustrated in Figure 2-5.
You set the extension as you would for any other document in a Cocoa document-based application and select the desired store type from the pop-up menu in the Store Type menu. Typically SQLite represents the best choice for a deployed application. 20 Creating the Project, Model, and Interface Build and Test Spotlight metadata importers are associated with document types by specifying the uniform type identifiers (UTIs) that they can extract data from (see Extracting Metadata from Documents). If you plan to write a custom importer you should therefore specify a suitable UTI for your document type (for details of how to specify a UTI, see Uniform Type Identifiers Overview ).
Figure 2-5 Properties pane of the Target Info window Build and Test You can now build and test your application. You should find that it is (almost) fully functional! You can add and remove employees, and edit their attributes. You can set an employee s manager using the pop-up menu. The application supports undo and redo, the search field works, and you can save and open documents. If you make a mistake and enter, for example, a negative salary, then try to save the document, the application presents an alert.
You haven t yet written any code. As you test the application, however, you may start to notice some problems. The popup menu displays only the employees first names.
First, this may not be the best way of identifying a particular employee. Second, if more than one employee share the same name, it is impossible to distinguish between them.
There are several steps that are necessary to fix these problems, all of which require a custom class for the Employee entity which in turn requires a change to the managed object model. Specifically, you need to do the following (the implementation of these steps is described in the next section). 20 21 Creating the Project, Model, and Interface What Happened? In a custom class for the Employee entity, define a custom method to return a unique derived property a string that contains the employee s full name and employee ID. Implement the relevant key-value observing method to ensure that the derived property is updated when any of its components is changed. Implement a method to set a new employee ID when a new employee is created.
What Happened? The title Creating the Project, Model, and Interface (page 13) is somewhat misleading. More than just creating the project, in a very few steps you have completed the main task goals. In the next chapters you will refine the implementation and provide additional user-friendly behavior. Now, though, it is worth briefly reviewing what has happened. The example so far illustrates the focus of Core Data.
Your primary tasks have been to specify the data models in your application, and to implement custom logic relating to the data. The Core Data framework (in conjunction with Cocoa bindings for the user interface) has provided the infrastructure to support most of the other required behaviors. Most of the Core Data infrastructure is hidden by the persistent document. NSPersistentDocument implements a number of methods to support integration with Core Data. From the perspective of the typical developer, the most important method is managedobjectcontext. The managed object context serves as the gateway to the rest of the Core Data infrastructure.
Nspersistentdocument Core Data Tutorial For Mac Mac
It also provides the document s undo manager. NSPersistentDocument takes care of creating and setting the type of the persistent store, saving and loading data, and configuring the Core Data stack. For many applications you do not need to customize its behavior further.
Later in this tutorial, however, you will override some of NSPersistentDocument's methods to customize the store. 21 22 Creating a Custom Class There are three parts to implementing and using a custom Employee class for the application. First, you create the class files and update the managed object model. Second, you implement the accessor method for the derived value, and ensure that key-value observing notifications are sent when any of the value s components changes. Third, you implement a method to initialize the employee ID for new employees.
The Employee Class 1. In Xcode, either create a new Objective-C class and set its superclass and other details manually, or create a default implementation of a managed object class using the data modeling tool. Both achieve the same result, but through different paths.
The significant advantage to using the modeling tool is that it can also auto-generate accessor and validation methods for you in this case, however, there's no need to do so. To create a custom managed object class manually, perform the following steps. Create a new Objective-C class. Call the class Employee.
In the Employee class s header file, change its superclass to NSManagedObject. To create a custom managed object class using the Xcode data modeling tool, perform the following steps: a. In Xcode, select the data model and ensure that it is the frontmost editor for example, simply click inside the model diagram view. (Xcode does not display the Managed Object Class option in the next step unless you do this.) b. Select File New File to show the New File Assistant. In the file type outline view select Design Managed Object Class and press Next.
In the subsequent pane select the current project and target, then again press Next. In the subsequent pane (Managed Object Class Generation), select the Employee entity and uncheck the relevant boxes to specify that the implementation should not contain custom accessor or validation methods. Press Finish.
Xcode creates the files for the Employee class. In the data model (use the entity detail pane, or edit the name directly in the Class column in the entity browser), change the class name for the Employee entity from NSManagedObject to Employee. 22 23 Creating a Custom Class Support for the Derived Value Support for the Derived Value The value of fullnameandid is a concatenation of, and hence dependent on, the values of lastname, firstname, and employeeid. To ensure that the derived value is updated whenever any of its components changes, you must invoke the key-value observing method that specifies that a key is dependent on others setkeys:triggerchangenotificationsfordependentkey. This method is typically invoked in a class s initialize method.
In the Employee class s header file, add a declaration for an instance method, -(NSString.)fullnameandid. (Note that this is the only modification other than the change to the superclass made in the first step that you need to make to the header file. In particular, there is no need to add any instance variables.) 2. In the implementation file, implement the fullnameandid method. It concatenates the first and last names and the employee ID, as illustrated in the example below.
(NSString.)fullnameandid return NSString self self self 3. In the Employee class, implement initialize as follows: + (void)initialize if (self Employee class) NSArray.keys = @'employeeid', nil; self setkeys:keys 23 24 Creating a Custom Class Initializing the Employee ID 4. In the nib file, change the contentvalues binding for the pop-up menu. Set the model key path to fullnameandid (the other values remain the same). You may also add fullnameandid to the data model as a transient string attribute. (In this case, since the value is solely read-only and dependent on other attributes, there is no functional benefit, but it is worth doing so that the model more fully communicates the entity s behavior.) Build and Test Build and run the application again.
You should find that the manager pop-up properly displays the full name and ID of each employee, and that the menu item titles update as you change any of the individual components. What the steps so far have not addressed, however, is the need to ensure that the value of fullnameandid is unique when new employees are added. Initializing the Employee ID The application could benefit from a means of initializing a managed object when it is created or more specifically, the first time it is added to the object graph.
The init method (more correctly, the class s designated initializer, initwithentity:insertintomanagedobjectcontext:) is not appropriate since it is called each time the object is instantiated (when it is first created, and whenever it is subsequently retrieved from the persistent store). Core Data provides a special initialization method, awakefrominsert, which is called once and only once in the lifetime of a managed object, on the first occasion it is inserted into a managed object context. This may be useful to, for example, set the creation date of a record. Contrast this with awakefromfetch, which is called on subsequent occasions an object is fetched from a data store. Implement awakefrominsert The following implementation is crude (and should not be used in a production application the initial tempid reverts to 1 every time the application is launched), it serves, however, to quickly illustrate the principle.
(void)awakefrominsert static int tempid = 1; super awakefrominsert; self setvalue:nsnumber numberwithint:tempid 24 25 Creating a Custom Class What Happened? Build and Test Build and run the application again. You should find that as new employees are added to the document, the employee ID is set, and the ID is incremented for each new employee. More importantly, it is now possible to differentiate between all the employees in the Managers pop-up menu.
What Happened? Most of the task goals have now been met primarily by creating a custom class to represent the Employee entity and implementing business logic. A subtle point here is the interaction between the model and the employees array controller.
Recall that you use the array controller to add new employees to the document. When the user interface was created, however, it was not configured to manage instances of a particular class. Instead it was configured to manage an entity (in this case, Employee). When you first built and tested the application, the model specified that employees should be represented by NSManagedObject. When the array controller created a new employee, therefore, it created a new instance of NSManagedObject (and set its entity description accordingly). After you updated the model, however, to specify that employees be represented by Employee, when the array controller created a new employee, it created a new instance of Employee. You will see in principle how this works in the next section when you create an instance of the Department entity.
Code Listing for the Employee Class The complete listing for the implementation of Employee class up to this point is given in Listing 3-1. 64 A Sheet for Creating a New Employee It is possible to use the existing application to create new employees. The Add button that is part of the automatically-created user interface is connected to the add: method of the Employee array controller. If you click on the button, a new instance of the array controller's entity is added to its content array.
Sometimes, though, you might want a more sophisticated user interface. Specifically, some applications use a sheet to allow the user to fill in details about a new entry, and optionally discard the new entry before it is committed. An example of how this could be implemented in the current project is illustrated in Figure 8-1 (page 64). Figure 8-1 Creating a new employee using a sheet 64 65 A Sheet for Creating a New Employee Design Considerations In following this section, recall that you are expected to have mastered fundamental Cocoa tools and techniques (see Introduction to NSPersistentDocument Core Data Tutorial for Mac OS X v10.4 (page 8)) basic tasks such as connecting outlets and establishing bindings in Interface Builder are not described in detail.
Important: This is a preliminary document. Although this document has been reviewed for technical accuracy, it is not final.
Newer versions of this document may be provided in the future. For information about updates to this and other developer documentation, view the New & Updated sidebars in subsequent releases of the Reference Library.
Design Considerations There are a number of issues to consider in implementing the sheet. To create a new employee instance, you need a managed object context. A more subtle but profound aspect is that you should isolate any changes from the document, so that adding the new employee is a single action that can be undone in a single action. Moreover, it should be possible (if the user clicks Cancel) to discard the new instance without affecting the undo stack in the document. Together, these constraints suggest a new pattern, where the sheet uses a separate managed object context. The new employee is inserted into this context when the sheet is created.
If the user clicks the Add button, the employee is added to the document's main context. If the user clicks the Cancel button, the new employee is deleted and the sheet's context reset the document's context remains unchanged. The implementation shown here is but one of several possibilities. Management of the sheet and the managed object context are factored into a separate controller, distinct from the document object. This largely follows the coordinator design pattern (discussed in The Model-View-Controller Design Pattern in Cocoa Fundamentals Guide ). The goal here, though, is primarily to illustrate the use of Core Data, with comparatively few distractions; it also aims to be fairly reusable. You can adapt what you learn in the tutorial to your own needs.
Which approach you take for your own project will depend on the constraints that are specific to your application. Implementation Overview There are several separate steps to the implementation. You need to define the functionality of a new class the new employee sheet controller and create a new nib file that contains the sheet.
The new employee controller needs to be able to do several things. It must coordinate its own managed object context which must be configured using the managed object model from the document. The controller must also be able to add the new employee to the document's Department object's employees relationship. To do this, it needs access to that relationship, and again to the document's managed object model. 65 66 A Sheet for Creating a New Employee Declaring and Setting up NewObjectSheetController The simplest way to satisfy all these needs is to give the new employee controller an outlet to the employees array controller. The array controller gives access to the document's managed object context (to which it is bound), gives the new employee controller an easy way to create a new Employee instance, and (since it manages it) a means to insert the new Employee into the Department's employees relationship. One issue with creating the new top-level object in the nib file is that when you use bindings an object retains other objects to which it is bound.
This means that bindings must be broken to ensure there are no retain cycles when a document is closed. Moreover, since the nib file the new controller owns contains top level objects and the controller s class does not inherit from NSWindowController, you need to release the top level objects when the window is closed. You need to support undo in the sheet. Declaring and Setting up NewObjectSheetController Create in the project the files for a new class called NewObjectSheetController. An instance of this class is responsible for creating and managing the sheet for the new Employee object and for coordinating data between the sheet and the document. The header file The sheet controller uses a private managed object context.
It also need to be able to access the document window, the employees array controller, an object controller in its own nib file, and the sheet. Add to the interface suitable instance variables for all NewObjectSheetController: NSObject IBOutlet NSWindow.documentwindow; IBOutlet NSArrayController.sourcearraycontroller; IBOutlet NSObjectController.newobjectcontroller; IBOutlet NSPanel.newobjectsheet; NSManagedObjectContext.managedobjectcontext; To complete the interface, add the method declarations. The controller needs to provide accessor methods for accessing the managed object contexts, and action methods to launch the sheet, dismiss the sheet, and support undo and redo. 66 67 A Sheet for Creating a New Employee Declaring and Setting up NewObjectSheetController - (NSManagedObjectContext.)managedobjectcontext; - (NSManagedObjectContext.)documentmanagedobjectcontext; - (IBAction)add:(id)sender; - (IBAction)addNewObjectFromSheet:sender; - (IBAction)cancelNewObjectFromSheet:sender; - (IBAction)undo:sender; - Update the Document nib File You need to add an instance of the sheet controller in the document nib file and configure it appropriately.
Import the NewObjectSheetController header file into MyDocument.nib, then instantiate an instance. Connect the outlets as follows: Connect documentwindow to the document window. Connect sourcearraycontroller to the Employees array controller. 68 A Sheet for Creating a New Employee Declaring and Setting up NewObjectSheetController Now disconnect the Add button from the array controller, and set its target to be the sheet controller and the action to be add. Create and Configure the Sheet Controller nib File The next stage is to create and configure the user interface for the sheet. Create a new nib file named NewEmployeeSheet and add it to the project.
Import the header file for the NewObjectSheetController class into the nib file. Set the File s Owner to be an instance of NewObjectSheetController. Add an NSObjectController instance to the nib file. Set its entity to be Employee.
Bind the managedobjectcontext binding of the NSObjectController instance to the managedobjectcontext key of File s Owner. Connect the File s Owner s newobjectcontroller outlet to the object controller. 69 A Sheet for Creating a New Employee Declaring and Setting up NewObjectSheetController Add an NSPanel instance to the nib file.
To the panel, add user interface elements so that it looks like the image below. Connect the File s Owner s newobjectsheet outlet to the panel. Connect the panel s delegate outlet to the File s Owner. Bind the values of the text fields to the appropriate attribute in the object controller. For example, bind the value of the First Name text field to the object controller s selection.firstname key path.
Ensure that the Validates Immediately option is selected. Set up the target and action for the Add and Cancel buttons. The target is File s Owner, the actions are addnewobjectfromsheet: and cancelnewobjectfromsheet: respectively.