UINavigationController *nc = [self.splitViewController.viewControllers lastObject];
self.detailViewController = (SplitViewDetailViewController *)[nc topViewController];
有一个splitViewController属性,其viewControllers为一个含两个ViewController的数组,0-left,1-right in landscape
看文档:https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UISplitViewController_class/Reference/Reference.html#//apple_ref/doc/uid/TP40009277
The array in this property must contain exactly two view controllers. The view controllers are presented left-to-right in the split view interface when it is in a landscape orientation. Thus, the view controller at index 0 is displayed on the left side and the view controller at index 1 is displayed on the right side of the interface.
The first view controller in this array is typically hidden when the device is in a portrait orientation. Assign a delegate object to the receiver if you want to coordinate the display of this view controller using a popover.
创建过程:
在StoryBoard中让Master Detail都Embed in navi..
然后先在AppDelegate的
didFinishLaunchingWithOptions中添加代码,使得splitView的delegate为DetailViewController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController; UINavigationController *navigationController = [splitViewController.viewControllers lastObject]; splitViewController.delegate = (id)navigationController.topViewController; return YES; }
在MasterVC中取得DetailVC
@property (strong, nonatomic) DetailViewController *detailViewController;
viewDidLoad:
UINavigationController *nc = [self.splitViewController.viewControllers lastObject];
self.detailViewController = (DetailViewController *)[nc topViewController];
self.detailViewController.detailItem = xxxx;
在DetailVC中,当然要遵循<UISplitViewControllerDelegate>
@property (strong, nonatomic) id detailItem;//in .h
@property (strong, nonatomic) UIPopoverController *masterPopoverController;//in .m
- (void)setDetailItem:(id)newDetailItem {
// Update the view.
}
#pragma mark - Split view - (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController { barButtonItem.title = @"列表"; [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; }
若DetailVC不止是一个ViewController,我们可以使MasterVC遵循<UISplitViewControllerDelegate>,实现其委托方法;让DetailVC遵循自定义的协议,其功能是使在land和port时隐藏\显示 调出popover的barItem
然后在tableView:didSelectRowAtIndexPath: 中实例化相应的DetailVC,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
FirstDetailViewController *newDetailViewController = [storyboard instantiateViewControllerWithIdentifier:@"v1"];
detailViewController = newDetailViewController;
当然也可以通过StoryBoard中的Segue代替实现实例化功能
本文转自老Zhan博客园博客,原文链接:http://www.cnblogs.com/mybkn/archive/2012/05/12/2497266.html,如需转载请自行联系原作者