Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Core Data iOS Essentials

You're reading from  Core Data iOS Essentials

Product type Book
Published in Apr 2011
Publisher Packt
ISBN-13 9781849690942
Pages 340 pages
Edition 1st Edition
Languages

Table of Contents (19) Chapters

Core Data iOS Essentials
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
1. Overview 2. Understanding Core Data 3. Understanding Objective-C Protocol and Table View 4. Designing a Data Model and Building Data Objects for Customers 5. Creating, Listing, and Deleting Names of Customers 6. Creating, Listing, Displaying, and Deleting Records of Customers 7. Updating and Searching Records of Customers 8. Entering, Saving, Listing, and Deleting the Records of the Products Sold to the Customers 9. Entering, Displaying, and Deleting the Stock 10. Editing the Stock Information 11. Displaying the Products for Sale and Updating the Stock Appendix

Chapter 3. Understanding Objective-C Protocol and Table View

In the previous chapter, we had a fair enough introduction of the Core Data framework and its components. We also learnt about the Data Model and its different terms including Entities, Properties, Attributes, Relationships, Inverse Relationships, and so on. We also explored the concepts of the Managed Object Model, Managed Object, and Managed Object Context. Finally, we saw the relationships among different components of Core Data API: Persistent Store, Persistent Store Coordinator, Fetch Request, and FetchedResultsController. Besides this, we also had a quick overview of the entire application that we will be building in this book from chapters 4 through 11.

As said in Chapter 1, Overview, for a better understanding of the Core Data, we need to have a good knowledge of two important concepts: How classes can adopt various protocols in Objective-C and knowledge of how information is displayed via Table View control. So, before...

Protocol


A protocol is not itself a class. Rather, it's an interface that declares methods. Only the methods are declared, that is, there is no body of the method defined in the protocol. The reason that format protocols are used for the delegation pattern is two fold:

  1. 1. The protocol acts as a documentation of the delegate interface and it allows for the compiler to check adherence.

  2. 2. At runtime you can interrogate an object in a single call to see if a protocol has been adopted rather than checking on a per method basis to see if an object will respond to a message.

The delegates are responsible for implementing the methods of the confirming protocol.

Implementing the Strategy pattern

To work with a protocol, we need to:

  1. 1. Define the protocol

  2. 2. Create the delegate property

  3. 3. Declare the protocol methods

Defining the protocol

A protocol is defined by using the @protocol compiler directive, combined with an @end directive. In between the two directives, we must declare the protocol method...

Creating a sample application using a protocol and a delegate


We will begin developing our actual application, Sales Record System for Departmental Store from Chapter 5, Creating, Listing, and Deleting Names of Customers. In this chapter, we will develop a small application that is meant for making you better understand the concept of protocol and delegate. The application that we are going to develop is a nice example of a class adopting a protocol. Here, we'll define a protocol called SecondViewControllerDelegate, which has two methods: sum and multiply. These two methods are used to calculate the sum and product of two numbers. The class that will conform to this protocol is called demodelegateViewController. It will implement the two protocol methods, sum and multiply. We will see how the delegate methods are invoked to calculate the sum and product of two numbers entered by the user:

  1. 1. Launch Xcode and select the New Project option from the File menu. The New Project Assistant window...

Introduction to Table View


We just saw how protocols are created and defined through a running example. We also saw the creation of the delegate property, methods, and the implementation of those methods. The second important concept that we will understand before developing Core Data application is the working of TableView control. The UITableView class is very useful and has support for core data operations out of the box. It is very popularly used to display information and to edit, delete, and insert new information too. As it is very important to have a sound knowledge of TableView control and its different methods that are used for displaying information, let us go ahead and look at the working of TableView control.

Table View plays a major role in iPhone applications because it's a primary choice for displaying lists, for example, available options, products, and so on. From lists displayed in a table view, users choose a selection and jump to another page for further information...

Creating an application to display Table View cells


The application that we are going to create in this chapter is focused on understanding the working of Table View control and has nothing to do with the actual application: Sales Record System for Departmental Store. From the next chapter, we will start developing the actual application. In this application, we'll learn how to display a list of names in a table view. The steps are as follows:

  1. 1. Create a new project by selecting the File | New Project option.

  2. 2. The New Project Assistant window appears, prompting us to select a template for the new project. Select the Navigation based Application template and click the Choose button.

  3. 3. A dialog box appears, asking us to assign a name and location for the new project. The default location is the Documents folder on our local drive, but we can specify a different location if we wish. Let's assign the name demotable to our new project and select the Save button.

Xcode will generate the project...

Adding names to the Table View


To add more names to the table view, we need to add a View Controller class that displays a navigation bar with two items: Save and Cancel. This View Controller class also displays a View that prompts the user to enter a name to be added. We also need to add code to the RootView controller implementation file, so that when the user selects the Save button after entering the new name, it's added to the Table View for display.

The sequence is as follows:

  1. 1. Add a View Controller class called AddNameController.

  2. 2. Define a protocol, outlets, and action methods in the AddNameController.h header file.

  3. 3. Define the View of the AddNameController class and the connecting controls.

  4. 4. Add code to the AddNameController.m implementation file to invoke delegate methods.

  5. 5. Invoke the newly added AddNameController View and implement the protocol methods.

  6. 6. Place a Bar Button Item Control in the RootViewController and connect it.

Adding the AddNameController View controller...

Summary


In this chapter, we examined the steps required for working with protocols — definition, creation, delegate property creation, methods, delegate declaration, and implementation methods. Also, we saw how Table View controls can be used to display array data. We also took a look at common methods for adding information to Table Views. In the next chapter, we are going to start building our application: Sales Record System for a Departmental Store. We will begin with the first step: Design Data Model for keeping the customer's information, that is, we will learn to define Customer entity and its attributes. Also, we will learn to build data object (classes) associated with the Customer entity.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Core Data iOS Essentials
Published in: Apr 2011 Publisher: Packt ISBN-13: 9781849690942
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}