Programming4us
         
 
 
Programming

iPhone Programming : Table-View-Based Applications - Adding Navigation Controls to the Application

5/27/2011 11:29:47 AM
Next, back out the changes you just made to the tableView:didSelectRowAtIndexPath: method by deleting the lines you added in the preceding section (be careful to not remove the call to deselectRowAtIndexPath).

Now, let’s wrap this app up properly. This means we have to add a UINavigationController to the application. If you’ve used many iPhone apps, you’ll be familiar with this interface; it’s one of the most commonly used iPhone design interface patterns. Clicking on a cell in the table view makes the current view slide to the left and a new view is displayed. You return to the original table view by clicking on the Back button.

The first thing you need to do is add an IBOutlet to a UINavigationController to the app delegate interface (CityGuideDelegate.h):

#import <UIKit/UIKit.h>

@class RootController;

@interface CityGuideDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootController *viewController;
NSMutableArray *cities;
UINavigationController *navController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet RootController *viewController;
@property (nonatomic, retain) IBOutlet UINavigationController *navController;
@property (nonatomic, retain) NSMutableArray *cities;

@end


You also need to make some modifications to the app delegate implementation (CityGuideDelegate.m). Add a new line of code near the top to @synthesize the new property:

@synthesize window;
@synthesize viewController;
@synthesize cities;
@synthesize navController;

Now you need to replace the section of the code that adds the RootController main view as a subview of the main window. Delete the following line from the bottom of the applicationDidFinishLaunching: method:

[window addSubview:viewController.view];

Next, replace it with the code shown in bold in the following code snippet. This new code adds the RootController to the NavController’s stack of view controllers, making its view the current view of the NavController. Then it sets the current NavController view as the subview of the main window. The end of the applicationDidFinishLaunching: method should look like this now:

    // Override point for customization after app launch
navController.viewControllers = [NSArray arrayWithObject:viewController];
[window addSubview:navController.view];
[window makeKeyAndVisible];
}


As the current view of the NavController changes, it will automatically update the subview of the main window, and thus what the user sees on his screen. Let’s get this working first, and afterward I’ll discuss exactly how the NavController manipulates its stack of views.

Open the MainWindow.xib file in Interface Builder and drag and drop a navigation controller (UINavigationController) into the main NIB window (titled “MainWindow” or “MainWindow.xib”). The navigation controller is found on the Library (⌘-Shift-L) under Cocoa TouchControllers.

After doing so, you should see something similar to Figure 1. Note the navigation bar that appears at the top (with the title “City Guide”).

After adding the UINavigationController to the NIB, click on the CityGuide App Delegate icon in the main NIB window and switch to the Connections pane (⌘-2) of the Inspector window. Connect the navController outlet to the UINavigationController, as shown in Figure 5-15.

After performing this step, save the NIB file and return to Xcode. Open the RootController.m file and add the following snippet at the top of the viewDidLoad: method:

self.title =  @"City Guide";

We’ve reached another good time to take a break, so click Build and Run. If you’ve followed all the steps, you should see what I see, something that looks a lot like Figure 3.

Figure 1. Adding a UINavigationController to the MainWindow.xib NIB file


Figure 2. Connecting the UINavigationController to the outlet created in the application delegate code earlier


Figure 3. The CityGuide application is starting to look more like an iPhone application after adding a navigation bar

Other -----------------
- iPad SDK : Popovers - Popover Preparations
- iPad SDK : Preparing Dudel for a New Tool (part 5) - Rendering Multiple Styles
- iPad SDK : Preparing Dudel for a New Tool (part 4) - Creating a New Drawable Class
- jQuery 1.3 : AJAX - Loading data on demand (part 3) - Loading an XML document
- jQuery 1.3 : AJAX - Loading data on demand (part 2) - Working with JavaScript objects
- jQuery 1.3 : AJAX - Loading data on demand (part 1) - Appending HTML
- Coding JavaScript for Mobile Browsers (part 13) - Zoom and rotate gestures
- Coding JavaScript for Mobile Browsers (part 12) - Swipe gesture
- Coding JavaScript for Mobile Browsers (part 11)
- Coding JavaScript for Mobile Browsers (part 10) - Event Handling
- iPad SDK : Preparing Dudel for a New Tool (part 3) - Creating the Text Tool
- iPad SDK : Preparing Dudel for a New Tool (part 2) - Implementing Changes to the Controller Class
- Coding JavaScript for Mobile Browsers (part 9) - Scripting Styles
- Coding JavaScript for Mobile Browsers (part 8) - DOM
- iPad SDK : Preparing Dudel for a New Tool (part 1) - Setting Up the GUI
- Coding JavaScript for Mobile Browsers (part 7)
- Coding JavaScript for Mobile Browsers (part 6)
- iPad SDK : The Structure of Core Text
- iPad SDK : PDF Generation
- jQuery 1.3 : Sorting and paging (part 5) - Finessing the sort keys
 
 
Most View
- Managing Websites with IIS Manager (part 3) - The ASP.NET Account
- Performing Administrative Tasks Using Central Administration (part 15) - E-Mail And Text Messages
- Building Android Apps : Animation - Adding the Settings Panel
- Scripting Windows 7 with WSH : Programming the Windows Management Instrumentation Service
- SOA with .NET and Windows Azure : Web Services (ASMX and WSE)
- Coding JavaScript for Mobile Browsers (part 8) - DOM
- Windows Phone 7 Game Development : The World of 3D Graphics - Rendering 3D Objects
- Managing Windows Server 2012 Storage and File Systems : Storage Management (part 11) - Managing GPT disk partitions on basic disks
- SharePoint 2010 : Publishing Service Applications to Remote Farms
- Manage Windows Server 2008 : Work with Preconfigured MMCs
Top 10
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 3) - Configuring Recipient Filtering
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 2)
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 1)
- Implementing Edge Services for an Exchange Server 2007 Environment : Installing and Configuring the Edge Transport Server Components
- What's New in SharePoint 2013 (part 7) - BCS
- What's New in SharePoint 2013 (part 6) - SEARCH
- What's New in SharePoint 2013 (part 6) - WEB CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 5) - ENTERPRISE CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 4) - WORKFLOWS
- What's New in SharePoint 2013 (part 3) - REMOTE EVENTS