Creating the Books Library user interface

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.

Getting ready…

In this section, we will start by building the components that will comprise of our user interface for our application.

How to do it...

To begin, follow these simple steps as outlined in the following order:

  1. From the Project Navigator window, select the file named MainStoryboard.storyboard.
  2. From the Object library, select and drag a new (UITableViewController) Table ViewController control, and add this to the view.
  3. Next, select the UITableViewController control that we just added, and then choose Editor | Embed In | Navigation Controller.
  4. On the Navigation Controller window, ensure that the Shows Navigation Bar and Shows Toolbar are both ticked under the Bar Visibility section of the Navigation Controller window.
  5. Next, select the UITableViewController control and then click on the Table View Cell tab, then choose the Prototype cell from the Prototype Cells section.
  6. From the Attributes Inspector section, change the Style to Subtitle. This will change the cell's appearance to contain two labels.
  7. Select the Identifier item and enter in BookCell as its unique identifier.
  8. Next, from the Object Library window, select and drag a (UIBarButtonItem), and add this to the left of the navigation bar on our Table View Controller.
  9. From the Attributes Inspector section, change the value of Title to Add.
  10. Add another UIBarButtonItem to the right of the navigation bar on our Table View Controller and change the value of Title to Sort Order.
  11. Add another UIBarButtonItem to the bottom left of the navigation bar on our Table View Controller and change the value of Title to Connect.
  12. Add a Flexible Space Bar Button item next to the Connect button.
  13. Add another UIBarButtonItem to the bottom right of the navigation bar on our Table View Controller and change the value of Title to Transfer.
  14. Next, we need to create our very own custom UITableViewController subclass that will act as the data source for our table.
  15. Select the BooksLibary folder, choose File | New | File… or press Command + N.
  16. Select the Objective-C class template from inside the Cocoa Touch group.
  17. Click on the Next button to proceed to the next step in the wizard.
  18. Enter in BooksViewController as the name of the file to create.
  19. Ensure that you have selected UITableViewController as the type of subclass to create from the Subclass of drop down.
  20. Next, click on the Next button to proceed to the next step in the wizard, and then click on the Create button to save the file.
  21. Next, select the MainStoryboard.storyboard file from the Project Navigator window.
  22. Select the UITableViewController control and click on the Identity Inspector section, change the value of the Custom Class property to read BooksViewController.
  23. Next, we are going to create and set up the 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.

  1. Open the BooksViewController.h interface file from the Project Navigator window.
  2. Next, modify the interface file as shown by the highlighted code sections.
    //  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
  3. Next, open the 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;
  4. Next, modify the 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;
    }
  5. Then, Build and Run the application by choosing Product | Run from the Product menu, or alternatively pressing Command + R.

How it works...

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.

See also

  • The Displaying data within the Table View recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset