/**
注意点:
1.看 GIF 效果图.
2.看连线视图的效果图.
3.看实现代码(直接复制实现效果).
*/
一、GIF 效果图:
二、连线视图的效果图:
图1:
图2:
三、实现代码:
控制器1:AppDelegate.h
#import @classSHDetailViewController,SHRootTableViewController; @interfaceAppDelegate :UIResponder @property(strong,nonatomic)UIWindow*window; //详情界面 @property(nonatomic,strong)SHDetailViewController*detailVC; //左侧边栏的表格界面 @property(nonatomic,strong)SHRootTableViewController*rootVC; //边栏控制器(自动添加了手势自动弹回功能) @property(nonatomic,strong)UISplitViewController*spiltVC; @end
控制器1:AppDelegate.m
#import"AppDelegate.h" #import"SHRootTableViewController.h" #import"SHDetailViewController.h" @interfaceAppDelegate() @end @implementationAppDelegate - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { self.rootVC= [[SHRootTableViewControlleralloc]initWithStyle:UITableViewStylePlain]; self.rootVC.navigationItem.title=@"导航视图"; UINavigationController*rootNav = [[UINavigationControlleralloc]initWithRootViewController:self.rootVC]; self.detailVC= [[SHDetailViewControlleralloc]init]; self.detailVC.navigationItem.title=@"详情视图"; UINavigationController*detailNav = [[UINavigationControlleralloc]initWithRootViewController:self.detailVC]; //使用iPad专用控件实现分栏效果 self.spiltVC= [[UISplitViewControlleralloc]init]; //设置分栏试图先后顺序决定了左右关系 self.spiltVC.viewControllers =@[rootNav,detailNav]; self.spiltVC.delegate =self.detailVC; //设置为窗口的跟视图控制器 self.window.rootViewController =self.spiltVC; returnYES; }
控制器2:
// iPad开发dome // // Created by石虎on 2017/8/7. // Copyright ? 2017年shihu. All rights reserved. // #import"SHRootTableViewController.h"//左侧边栏的表格界面 #import"SHDetailViewController.h" #import"AppDelegate.h" @interfaceSHRootTableViewController() //图片名称数组 @property(nonatomic,strong)NSArray*imgTitleArr; //图片数组 @property(nonatomic,strong)NSArray*imgArr; @end @implementationSHRootTableViewController - (void)viewDidLoad { [superviewDidLoad]; self.imgTitleArr=@[@"大众",@"法拉利",@"宝马",@"奔驰",@"JEEP",@"迈巴赫",@"兰博基尼"]; //通过循环将图片数组初始化 NSMutableArray*arr = [[NSMutableArrayalloc]init]; for(inti =1; i <=7; i++) { NSString*imgName = [NSStringstringWithFormat:@"car%d.jpg",i]; UIImage*img = [UIImageimageNamed:imgName]; [arr addObject:img]; } // self.imgArr = [arr copy]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { returnself.imgArr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { staticNSString *identifier =@"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; //缓存池 if(cell ==nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; } cell.textLabel.text =self.imgTitleArr[indexPath.row]; returncell; } //跳转 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //得到在Appdele中实例化的DetailViewController对象的内存 AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; SHDetailViewController *detaiVC = appDelegate.detailVC; detaiVC.imgView.image =self.imgArr[indexPath.row]; } @end
控制器3:SHDetailViewController.h
#import @interfaceSHDetailViewController :UIViewController //显示图片 @property(strong,nonatomic)IBOutletUIImageView*imgView; @end
控制器3:SHDetailViewController.m
#import"SHDetailViewController.h" #import"AppDelegate.h" #import"SHRootTableViewController.h"//左侧边栏的表格界面 @interfaceSHDetailViewController() @end @implementationSHDetailViewController - (void)viewDidLoad { [superviewDidLoad]; //导航栏 self.navigationItem.rightBarButtonItem= [[UIBarButtonItemalloc]initWithTitle:@"主菜单"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(showPopOverController:)]; AppDelegate*app = (AppDelegate*)[UIApplicationsharedApplication].delegate; app.spiltVC.displayModeButtonItem.title=@"显示导航栏"; self.navigationItem.leftBarButtonItem = app.spiltVC.displayModeButtonItem; } //回调方法 -(void)showPopOverController:(UIBarButtonItem *)sender { //左侧边栏的表格界面 SHRootTableViewController *rootVC = [[SHRootTableViewController alloc] initWithStyle:UITableViewStylePlain]; //弹出视图 UIPopoverController *popCtl = [[UIPopoverController alloc] initWithContentViewController:rootVC]; //弹出视图大小 popCtl.popoverContentSize = CGSizeMake(200,300); popCtl.backgroundColor = [UIColor yellowColor]; popCtl.delegate =self; //弹出该视图 [popCtl presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; } #pragma mark - UIPopoverControllerDelegate - (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController { NSLog(@"将要隐藏弹出视图"); returnYES; } - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController { NSLog(@"弹出视图已经隐藏"); } #pragma mark - UISplitViewControllerDelegate //左侧导航栏将要出现或隐藏时回调此方法 -(void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode { //左侧导航栏隐藏 if(displayMode == UISplitViewControllerDisplayModePrimaryHidden) { NSLog(@"左侧导航将要隐藏"); AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate; app.spiltVC.displayModeButtonItem.title =@"显示导航栏"; // svc.displayModeButtonItem.title =@"显示导航栏"; self.navigationItem.leftBarButtonItem = app.spiltVC.displayModeButtonItem; } elseif(displayMode == UISplitViewControllerDisplayModePrimaryOverlay) { NSLog(@"左侧导航覆盖到详情视图上"); } elseif(displayMode == UISplitViewControllerDisplayModeAllVisible) {NSLog(@"左侧导航全部显示"); self.navigationItem.leftBarButtonItem =nil; }else{NSLog(@"自动显示");}} @end
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。