shouldHideViewController:(UIViewController *)master inOrientation:(UIInterfaceOrientation)orientation數組
{app
return YES; // always hide itide
}佈局
{post
return UIInterfaceOrientationIsPortrait(orientation); //豎屏時隱藏masteratom
}spa
{ 代理
//將要隱藏master時,在detail控制器的toolbar上設置並顯示一個按鈕code
barButtonItem.title = @「Master」;對象
[detailViewController setSplitViewBarButtonItem:barButtonItem];
}
{
// removeSplitViewBarButtonItem: must remove the bar button from its toolbar
[detailViewController removeSplitViewBarButtonItem:nil];
}
<4>在iPad上的基本樣式截圖爲:
<5>在故事板佈局的樣式截圖爲:
下面咱們就經過純代碼的方式建立以下:
一、建立兩個控制器類,一個爲主控制器類MasterViewController,一個爲詳細控制器類DetailViewController
二、導入幾張素材圖片,用來在詳細控制器中顯示。全部的文件截圖爲:
三、下面就是具體的代碼建立了:
//在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主控制器: