iOS:分割控制器UISplitViewcontroller
shouldHideViewController:(UIViewController *)master inOrientation:(UIInterfaceOrientation)orientation
{
return YES; // always hide it
}
{
return UIInterfaceOrientationIsPortrait(orientation); //竖屏时隐藏master
}
{
//将要隐藏master时,在detail控制器的toolbar上设置并显示一个按钮
barButtonItem.title = @“Master”;
[detailViewController setSplitViewBarButtonItem:barButtonItem];
}
{
// removeSplitViewBarButtonItem: must remove the bar button from its toolbar
[detailViewController removeSplitViewBarButtonItem:nil];
}
<4>在iPad上的基本样式截图为:
<5>在故事板布局的样式截图为:
下面我们就通过纯代码的方式创建如下:
1、创建两个控制器类,一个为主控制器类MasterViewController,一个为详细控制器类DetailViewController
2、导入几张素材图片,用来在详细控制器中显示。所有的文件截图为:
3、下面就是具体的代码创建了:
//在AppDelegate.m文件中:
导入头文件并声明必要的属性,同时实现分割控制器的协议
#import "AppDelegate.h" #import "MasterViewController.h" #import "DetailViewController.h" @interface AppDelegate ()<UISplitViewControllerDelegate> @property (strong,nonatomic)UISplitViewController *splitViewController; //声明分割控制器 @end
创建分割控制器、主控制器、详细控制器,并设置它们之间的关系,以及设置分割控制器的代理
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //创建分割控制器 self.splitViewController = [[UISplitViewController alloc]init]; //创建MasterVC MasterViewController *MasterVC = [[MasterViewController alloc]init]; //创建DetailVC DetailViewController *DetailVC = [[DetailViewController alloc]init]; //创建左侧导航控制器 UINavigationController *MasterNavigationController = [[UINavigationController alloc]initWithRootViewController:MasterVC]; //创建右侧导航栏控制器 UINavigationController *DetailNavigationController = [[UINavigationController alloc]initWithRootViewController:DetailVC]; // 设置 UISplitViewController 所管理的左、右两个 UIViewController self.splitViewController.viewControllers = @[MasterNavigationController,DetailNavigationController]; //设置分割控制器分割模式 self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden; //设置代理 self.splitViewController.delegate = self; //设置window的根控制器 self.window.rootViewController = self.splitViewController; return YES; }
实现分割控制器协议的方法
#pragma mark -<UISplitViewController> //主控制器将要隐藏时触发的方法 -(void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc { barButtonItem.title = @"Master"; //master将要隐藏时,给detail设置一个返回按钮 UINavigationController *Nav = [self.splitViewController.viewControllers lastObject]; DetailViewController *Detail = (DetailViewController *)[Nav topViewController]; Detail.navigationItem.leftBarButtonItem = barButtonItem; } //开始时取消二级控制器,只显示详细控制器 - (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController:(UIViewController *)secondaryViewController ontoPrimaryViewController:(UIViewController *)primaryViewController { return YES; } //主控制器将要显示时触发的方法 -(void)splitViewController:(UISplitViewController *)sender willShowViewController:(UIViewController *)master invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
//master将要显示时,取消detail的返回按钮 UINavigationController *Nav = [self.splitViewController.viewControllers lastObject]; DetailViewController *Detail = (DetailViewController *)[Nav topViewController]; Detail.navigationItem.leftBarButtonItem = nil; }
//在MasterViewcontroller.m文件中:
导入头文件并声明必要的属性,同时实现分割控制器的协议
#import "MasterViewController.h" #import "DetailViewController.h" @interface MasterViewController ()<UITableViewDataSource,UITableViewDelegate> @property(strong,nonatomic)UITableView *tableView; //表格视图 @property (strong,nonatomic)NSMutableArray *dataObjects; //文字数据 @property (strong,nonatomic)NSMutableArray *imageArrayM; //图像数据 @end
创建主控制器Master的导航栏和按钮,并设置表格视图的数据源和代理
- (void)viewDidLoad { [super viewDidLoad]; //创建UITableView self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; //创建数组 self.dataObjects = [NSMutableArray arrayWithObjects:@"美女0",@"美女1",@"美女2",nil]; self.imageArrayM = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"美女0.jpg"],[UIImage imageNamed:@"美女1.jpg"],[UIImage imageNamed:@"美女2.jpg"],nil]; //设置数据源和代理 self.tableView.dataSource = self; self.tableView.delegate = self; [self.view addSubview:self.tableView]; //设置主控制器Master的导航栏和按钮 self.navigationItem.title = @"Master"; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:nil]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil]; }
实现表格视图的数据源协议方法
#pragma mark -<UITableViewDataSource> //多少行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataObjects.count; } //设置每一个单元格的内容 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //1.根据reuseIdentifier,先到对象池中去找重用的单元格对象 static NSString *reuseIdentifier = @"Cell"; UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; //2.如果没有找到,自己创建单元格对象 if(cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]; } //3.设置单元格对象的内容 cell.textLabel.text = [self.dataObjects objectAtIndex:indexPath.row]; return cell; }
实现表格视图的代理协议方法
#pragma mark -<UITableViewDelegate> //选中单元格时,设置详细控制器中的内容 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //获取详细控制器 UINavigationController *detailNAV = [self.splitViewController.viewControllers lastObject]; DetailViewController *detatilVC = (DetailViewController*)[detailNAV topViewController]; //创建图像视图 UIImageView *imageView = [[UIImageView alloc]initWithFrame:detatilVC.view.frame]; [imageView setImage:[self.imageArrayM objectAtIndex:indexPath.row]]; [detatilVC.view addSubview:imageView]; }
//在DEtailViewController.m文件中
设置视图背景颜色
- (void)viewDidLoad { [super viewDidLoad]; //设置视图颜色为白色 [self.view setBackgroundColor:[UIColor whiteColor]]; }
演示结果如下:
开始时:
点击Master,显示Master主控制器:
点击单元格时,Detail详细控制器显示的内容:
点击屏幕,关闭Master主控制器: