splitAppDelegate.h
#import <UIKit/UIKit.h> @class MasterViewController; @class DetailViewController; @interface splitAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; UISplitViewController *splitViewController; MasterViewController *masterViewController; DetailViewController *detailViewController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) UISplitViewController *splitViewController; @property (nonatomic, retain) MasterViewController *masterViewController; @property (nonatomic, retain) DetailViewController *detailViewController; @end
splitAppDelegate.m
#import "splitAppDelegate.h" #import "MasterViewController.h" #import "DetailViewController.h" @implementation splitAppDelegate @synthesize window, splitViewController, masterViewController, detailViewController; #pragma mark - #pragma mark Application lifecycle - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { masterViewController = [[MasterViewController alloc] initWithStyle:UITableViewStylePlain]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController]; detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil]; masterViewController.detailViewController = detailViewController; splitViewController = [[UISplitViewController alloc] init]; splitViewController.viewControllers = [NSArray arrayWithObjects:navigationController, detailViewController, nil]; splitViewController.delegate = detailViewController; [masterViewController release]; [detailViewController release]; [navigationController release]; [window addSubview:splitViewController.view]; [window makeKeyAndVisible]; return YES; } #pragma mark - #pragma mark Memory management - (void)dealloc { [splitViewController release]; [window release]; [super dealloc]; } @end
MasterViewController.h
#import <UIKit/UIKit.h> @class DetailViewController; @interface MasterViewController : UITableViewController { DetailViewController *detailViewController; NSMutableArray * fruits; } @property (nonatomic, retain) IBOutlet DetailViewController *detailViewController; @property (nonatomic, retain) NSMutableArray *fruits; @end
MasterViewController.m
#import "MasterViewController.h" #import "DetailViewController.h" @implementation MasterViewController @synthesize detailViewController; @synthesize fruits; #pragma mark - #pragma mark Size for popover - (CGSize)contentSizeForViewInPopoverView { return CGSizeMake(200.0, 400.0); } #pragma mark - #pragma mark View lifecycle - (void)viewDidLoad { [super viewDidLoad]; self.fruits = [[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"fruits" ofType:@"plist"]] retain]; } #pragma mark - #pragma mark Table view data source - (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section { return [fruits count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; cell.accessoryType = UITableViewCellAccessoryNone; } cell.textLabel.text = [self.fruits objectAtIndex:indexPath.row]; return cell; } #pragma mark - #pragma mark Table view delegate - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { detailViewController.detailItem = [self.fruits objectAtIndex: indexPath.row]; } #pragma mark - #pragma mark Memory management - (void)dealloc { [detailViewController release]; [super dealloc]; } @end
DetailViewController.h
#import <UIKit/UIKit.h> @interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate> { UIPopoverController *popoverController; UINavigationBar *navigationBar; id detailItem; IBOutlet UIImageView *fruitImageView; } @property (nonatomic, retain) UIPopoverController *popoverController; @property (nonatomic, retain) IBOutlet UINavigationBar *navigationBar; @property (nonatomic, retain) id detailItem; @property (nonatomic, retain) IBOutlet UIImageView *fruitImageView; @end
DetailViewController.m
#import "DetailViewController.h" #import "MasterViewController.h" @implementation DetailViewController @synthesize navigationBar, popoverController, detailItem,fruitImageView; #pragma mark - #pragma mark Managing the popover controller - (void)setDetailItem:(id)newDetailItem { if (detailItem != newDetailItem) { [detailItem release]; detailItem = [newDetailItem retain]; navigationBar.topItem.title = detailItem; NSString * imageName = [NSString stringWithFormat:@"%@.png",detailItem]; [self.fruitImageView setImage:[UIImage imageNamed:imageName]]; } if (popoverController != nil) { [popoverController dismissPopoverAnimated:YES]; } } #pragma mark - #pragma mark Split view support - (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc { barButtonItem.title = @"Master List"; [navigationBar.topItem setLeftBarButtonItem:barButtonItem animated:YES]; self.popoverController = pc; } - (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem { self.popoverController = nil; } - (void)viewDidUnload { self.popoverController = nil; } #pragma mark - #pragma mark Memory management - (void)dealloc { [popoverController release]; [navigationBar release]; [detailItem release]; [super dealloc]; } @end
示例图: