1. 首先create a single view project。然后删除ViewController .m, .h, .xib files。
2. 创建2个class with xib (parent class 均为UIViewController)
DetailViewController .m, .h, .xib
MasterViewController .m, .h, .xib
3. DetailViewController.h 实现 UISplitViewControllerDelegate protocol
DetailViewController.h
@interface DetailViewController : UIViewController <UISplitViewControllerDelegate>
@interface DetailViewController () @property (strong, nonatomic) UIPopoverController *masterPopoverController; @end @implementation DetailViewController @synthesize masterPopoverController = _masterPopoverController; .... #pragma mark - Split view - (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController { barButtonItem.title = NSLocalizedString(@"Master", @"Master"); [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES]; self.masterPopoverController = popoverController; } - (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem { // Called when the view is shown again in the split view, invalidating the button and popover controller. [self.navigationItem setLeftBarButtonItem:nil animated:YES]; self.masterPopoverController = nil; } @end
@interface MasterViewController : UIViewController @property (strong, nonatomic) DetailViewController *detailViewController; @end
6. MasterViewController.m则需要在initWithNibName方法中要设置其在popover中的尺寸
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0); } return self; }
7. 最重要的AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil]; UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController]; DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController]; masterViewController.detailViewController = detailViewController; self.splitViewController = [[UISplitViewController alloc] init]; self.splitViewController.delegate = detailViewController; self.splitViewController.viewControllers = [NSArray arrayWithObjects:masterNavigationController, detailNavigationController, nil]; self.window.rootViewController = self.splitViewController; [self.window makeKeyAndVisible]; return YES; }