The Split View-based Application template allows you to create a split-view interface for your application, which is essentially a master-detail interface. The left side of the screen displays a list of selectable items where the right-side displays the details of the item selected. This article shows you how you can develop a Split View-based application for the iPad.
Getting Started
Using Xcode, create a Split View-based Application project and name it splitViewBased (see Figure 1).Observe the files created in the Classes and Resources folder (see Figure 2). Notice that there are two View Controller classes (RootViewController and DetailViewController) as well as two XIB files (MainWindow.xib and DetailView.xib).
Press Command-R in Xcode to test the application on the iPad Simulator. Figure 3 shows the application when it is displayed in landscape mode. When you rotate the simulator to portrait mode, the application now looks like Figure 4. The list of items is now displayed in a PopoverView, a new View available only for the iPad.
Dissecting the Split View-based Application
The magic of a Split View-based Application lies in its transformation when the device is rotated. When in landscape mode, the application displays a list of rows on the left. When it is turned to portrait mode, the list of rows is now hidden in a Popover view. Let's see how this is done.First, observe the content of the splitViewBasedAppAppDelegate.h file:
#import <UIKit/UIKit.h> @class RootViewController; @class DetailViewController; @interface splitViewBasedAppAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; UISplitViewController *splitViewController; RootViewController *rootViewController; DetailViewController *detailViewController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic,retain) IBOutlet UISplitViewController *splitViewController; @property (nonatomic,retain) IBOutlet RootViewController *rootViewController; @property (nonatomic,retain) IBOutlet DetailViewController *detailViewController; @end
Next, observe the content of the splitViewBasedAppAppDelegate.m file:
#import "splitViewBasedAppAppDelegate.h" #import "RootViewController.h" #import "DetailViewController.h" @implementation splitViewBasedAppAppDelegate @synthesize window, splitViewController, rootViewController, detailViewController; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after app launch // Add the split view controller's view to the window and display. [window addSubview:splitViewController.view]; [window makeKeyAndVisible]; return YES; } - (void)applicationWillTerminate:(UIApplication *)application { // Save data if appropriate } - (void)dealloc { [splitViewController release]; [window release]; [super dealloc]; } @end
Now, double-click on the MainWindow.xib file to edit it in Interface Builder. Observe that the MainWindow.xib contains an item named Split View Controller (recall that for a View-based Application project, you will have a View Controller item instead).
Switch the MainWindow.xib file to display in List view mode and observe the various items located within the Split View Controller item (see Figure 5).
The Split View Controller item contains the following items:
- Navigation Controller
- Detail View Controller
Split View-based application
The Root View Controller is mapped to the RootViewController class (see Figure 7).The Detail View Controller controls the right side of a split view application (see Figure 8).
The application delegate is connected to the various view controllers, as you can see when you right-click on the Split View Based App App Delegate item (see Figure 10).
Let's examine the two view controllers that are contained within the Split View Controller - RootViewController and DetailViewController.
Observe the content of the RootViewController.h file:
#import <UIKit/UIKit.h> @class DetailViewController; @interface RootViewController : UITableViewController { DetailViewController *detailViewController; } @property (nonatomic, retain) IBOutlet DetailViewController *detailViewController; @end
The content of the RootViewController.m file contains many methods related to the Table view; here is a quick summary of some of the important methods:
- contentSizeForViewInPopoverView - the size of the PopoverView to display.
- numberOfSectionsInTableView: - the number of sections to be displayed in the Table view.
- tableView:numberOfRowsInSection: - the number of rows to be displayed in the Table view.
- tableView:cellForRowAtIndexPath: - the data to populate for each row.
- tableView:didSelectRowAtIndexPath: - the row that was selected by the user.
#import <UIKit/UIKit.h> @interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate> { UIPopoverController *popoverController; UIToolbar *toolbar; id detailItem; UILabel *detailDescriptionLabel; } @property (nonatomic, retain) IBOutlet UIToolbar *toolbar; @property (nonatomic, retain) id detailItem; @property (nonatomic, retain) IBOutlet UILabel *detailDescriptionLabel; @end
- UIPopoverControllerDelegate - It needs to implement this protocol because when the iPad is held in the portrait orientation, the PopoverView will display the content of the Table view.
- UISplitViewControllerDelegate - It needs to implement this protocol because when the iPad changes orientation, it needs to hide/display the PopoverView.
#import "DetailViewController.h" #import "RootViewController.h" @interface DetailViewController () @property (nonatomic, retain) UIPopoverController *popoverController; - (void)configureView; @end @implementation DetailViewController @synthesize toolbar, popoverController, detailItem, detailDescriptionLabel; /* ---Other commented out code are omitted from this code listing--- */ /* When setting the detail item, update the view and dismiss the popover controller if it's showing. */ - (void)setDetailItem:(id)newDetailItem { if (detailItem != newDetailItem) { [detailItem release]; detailItem = [newDetailItem retain]; // Update the view. [self configureView]; } if (popoverController != nil) { [popoverController dismissPopoverAnimated:YES]; } } - (void)configureView { // Update the user interface for the detail item. detailDescriptionLabel.text = [detailItem description]; } - (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc { barButtonItem.title = @"Root List"; NSMutableArray *items = [[toolbar items] mutableCopy]; [items insertObject:barButtonItem atIndex:0]; [toolbar setItems:items animated:YES]; [items release]; self.popoverController = pc; } // Called when the view is shown again in the split view, // invalidating the button and popover controller. - (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem { NSMutableArray *items = [[toolbar items] mutableCopy]; [items removeObjectAtIndex:0]; [toolbar setItems:items animated:YES]; [items release]; self.popoverController = nil; } - (void)dealloc { [popoverController release]; [toolbar release]; [detailItem release]; [detailDescriptionLabel release]; [super dealloc]; } @end
- splitViewController:willHideViewController:withBarButtonItem:forPopoverController: - fired when the iPad switches over to portrait mode (where the PopoverView will be shown and the Table View will be hidden).
- splitViewController:willShowViewController:invalidatingBarButtonItem: - fired when the iPad switches over to landscape mode (where the PopoverView will be hidden and the Table View will be shown).
Displaying a list of Movies
Now that you have seen a Split View-based Application in action, it is now time to make some changes to it and see how it is useful for the iPad. You will modify the application to show a list of movies and when a movie is selected, a picture will be displayed on the detail view.Double-click the DetailView.xib file to edit it in Interface Builder.
Add an ImageView to the View window and set its Mode to Aspect Fit in the Attributes Inspector window (see Figure 11).
In the Size Inspector window, set is Autosizing attribute as shown in Figure 12.
In Xcode, add the images as shown in Figure 13 to the Resources folder.
In the DetailViewController.h file, insert the following statements in bold:
#import <UIKit/UIKit.h> @interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate> { UIPopoverController *popoverController; UIToolbar *toolbar; id detailItem; UILabel *detailDescriptionLabel; IBOutlet UIImageView *imageView; } @property (nonatomic, retain) IBOutlet UIToolbar *toolbar; @property (nonatomic, retain) id detailItem; @property (nonatomic, retain) IBOutlet UILabel *detailDescriptionLabel; @property (nonatomic, retain) UIImageView *imageView; @end
Add the following statements in bold to the RootViewController.m file:
#import "RootViewController.h" #import "DetailViewController.h" @implementation RootViewController @synthesize detailViewController; NSMutableArray *listOfMovies; - (void)viewDidLoad { //---initialize the array--- listOfMovies = [[NSMutableArray alloc] init]; [listOfMovies addObject:@"Training Day"]; [listOfMovies addObject:@"Remember the Titans"]; [listOfMovies addObject:@"John Q."]; [listOfMovies addObject:@"The Bone Collector"]; [listOfMovies addObject:@"Ricochet"]; [listOfMovies addObject:@"The Siege"]; [listOfMovies addObject:@"Malcolm X"]; [listOfMovies addObject:@"Antwone Fisher"]; [listOfMovies addObject:@"Courage Under Fire"]; [listOfMovies addObject:@"He Got Game"]; [listOfMovies addObject:@"The Pelican Brief"]; [listOfMovies addObject:@"Glory"]; [listOfMovies addObject:@"The Preacher's Wife"]; //---set the title--- self.navigationItem.title = @"Movies"; [super viewDidLoad]; self.clearsSelectionOnViewWillAppear = NO; self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0); } - (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. //return 10; return [listOfMovies count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; // Dequeue or create a cell of the appropriate type. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; cell.accessoryType = UITableViewCellAccessoryNone; } // Configure the cell. // cell.textLabel.text = // [NSString stringWithFormat:@"Row %d", indexPath.row]; cell.textLabel.text = [listOfMovies objectAtIndex:indexPath.row]; return cell; } - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { /* When a row is selected, set the detail view controller's detail item to the item associated with the selected row. */ //detailViewController.detailItem = // [NSString stringWithFormat:@"Row %d", indexPath.row]; detailViewController.detailItem = [NSString stringWithFormat:@"%@", [listOfMovies objectAtIndex:indexPath.row]]; } - (void)dealloc { [listOfMovies release]; [super dealloc]; }
The value returned by the tableView:numberOfRowsInSection: method sets the number of rows to be displayed, and this case it is the size of the mutable array. The tableView:cellForRowAtIndexPath: method is fired for each item in the mutable array, thereby populating the Table view.
When an item is selected in the Table view, you will pass the movie selected to the DetailViewController object via its detailItem property.
Add the following statements in bold to the DetailViewController.m file:
#import "DetailViewController.h" #import "RootViewController.h" @interface DetailViewController () @property (nonatomic, retain) UIPopoverController *popoverController; - (void)configureView; @end @implementation DetailViewController @synthesize toolbar, popoverController, detailItem, detailDescriptionLabel; @synthesize imageView; /* When setting the detail item, update the view and dismiss the popover controller if it's showing. */ - (void)setDetailItem:(id)newDetailItem { if (detailItem != newDetailItem) { [detailItem release]; detailItem = [newDetailItem retain]; // Update the view. NSString *imageName = [NSString stringWithFormat:@"%@.jpg", [detailItem description]]; imageView.image = [UIImage imageNamed:imageName]; [self configureView]; } if (popoverController != nil) { [popoverController dismissPopoverAnimated:YES]; } }
Press Command-R to test the application on the iPhone Simulator. The following shows that when the simulator is in landscape mode, the application shows a list of movies on the left of the application (see Figure 15). Selecting a movie displays the movie image. You can also switch to portrait mode and select the movies from the PopoverView (see Figure 16).
没有评论:
发表评论