In this recipe we will learn how to build the user interface for our Books Library project, as well as creating a custom table view controller to act as the data source.
In this section, we will start by building the components that will comprise of our user interface for our application.
To begin, follow these simple steps as outlined in the following order:
MainStoryboard.storyboard
.UITableViewController
) Table ViewController control, and add this to the view.UITableViewController
control that we just added, and then choose Editor | Embed In | Navigation Controller.UITableViewController
control and then click on the Table View Cell tab, then choose the Prototype cell from the Prototype Cells section.UIBarButtonItem
), and add this to the left of the navigation bar on our Table View Controller.UIBarButtonItem
to the right of the navigation bar on our Table View Controller and change the value of Title to Sort Order.UIBarButtonItem
to the bottom left of the navigation bar on our Table View Controller and change the value of Title to Connect.UIBarButtonItem
to the bottom right of the navigation bar on our Table View Controller and change the value of Title to Transfer.UITableViewController
subclass that will act as the data source for our table.BooksLibary
folder, choose File | New | File… or press Command + N.MainStoryboard.storyboard
file from the Project Navigator window.UITableViewController
control and click on the Identity Inspector section, change the value of the Custom Class property to read BooksViewController
.outlets
and action
methods for each of our buttons, with the following names: btnAdd
, btnSortOrder
, btnConnect
, and btnTransfer
.Next, we need to set up references for both the NSManagedObjectContext
and NSFetchedResultsController
objects.
BooksViewController.h
interface file from the Project Navigator window.// BooksViewController.h // BooksLibrary // Created by Steven F Daniel on 03/12/12. // Copyright (c) 2012 GenieSoft Studios. All rights reserved. #import <UIKit/UIKit.h> #import "Books.h" @interface BooksViewController : UITableViewController { NSManagedObjectContext *managedObjectContext; NSFetchedResultsController *fetchedResultsController; NSArray *fetchedObjects; IBOutlet UIBarButtonItem *btnAdd; IBOutlet UIBarButtonItem *btnSortOrder; IBOutlet UIBarButtonItem *btnConnect; IBOutlet UIBarButtonItem *btnTransfer; // Fields for our btnAdd UIAlertView dialog UITextField *bookTitle; UITextField *bookAuthor; UITextField *bookPublisher; } // Core Data session objects @property (strong, nonatomic) NSManagedObjectContext *managedObjectContext; @property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController; // Property getters and setters for our UI @property (strong, nonatomic) IBOutlet UIBarButtonItem *btnAdd; @property (strong, nonatomic) IBOutlet UIBarButtonItem *btnSortOrder; @property (strong, nonatomic) IBOutlet UIBarButtonItem *btnConnect; @property (strong, nonatomic) IBOutlet UIBarButtonItem *btnTransfer; // Create the class instance methods -(void)populateBookDetails; - (IBAction)btnAdd:(id)sender; - (IBAction)btnSortOrder:(id)sender; - (IBAction)btnConnect:(id)sender; - (IBAction)btnTransfer:(id)sender; @end
AppDelegate.m
implementation file, located within the BooksLibrary folder, and enter in the following highlighted code.// AppDelegate.m
// BooksLibrary
// Created by Steven F Daniel on 03/12/12.
// Copyright (c) 2012 GenieSoft Studios. All rights reserved.
#import "AppDelegate.h"
#import "BooksViewController.h"
@implementationAppDelegate
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator =
_persistentStoreCoordinator;
didFinishLaunchingWithOptions
method, located within the AppDelegate.m
implementation file.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Point our Books View Controller to our data-model UINavigationController *navigationController = (UINavigationController *) self.window.rootViewController; BooksViewController *booksViewController = [[navigationController viewControllers]objectAtIndex:0]; booksViewController.managedObjectContext = self.managedObjectContext; return YES; }
In this recipe, we started by building our user interface for our BooksLibrary application, as well as properly configuring our Table View Controller. We then created our very own custom UITableViewController
subclass that will act as the data source for our table so that it will know how many rows to be displayed when it retrieves the information from our database.
Next, we updated the class of our UITableViewController
to use our newly created class instead of the default UITableViewController
class, and included a reference to the NSManagedObjectContext
and NSFetchedResultsController
objects, that provides us with all of the Core Data fetch-related functions, needed to perform when populating our table view with data. These functions encapsulate the common functions that are associated with the table and the Core Data model. We created an NSArray
array property that is used to store the retrieved data.
Finally, we initialized the data source for our booksViewController
using the managedObjectContext
method. This ensures that our controller has access to all of the required properties and methods required to add and retrieve the information from our data model. Before this can happen, we need to first cycle through each scene within our Storyboard in order to get a reference to the BooksViewController
. This is so that we can initialize its data source, so that it points to our database.