創(chuàng)建一個(gè)繼承于UITableViewController的控制器和繼承于UIViewController(綁定xib)的控制器
@class DetailViewController,RootTableViewController;@interface AppDelegate : UIResponder@property (strong, nonatomic) UIWindow *window;
//詳情界面
@property(nonatomic,strong)DetailViewController *detailVC;
//左側(cè)邊欄的表格界面
@property(nonatomic,strong)RootTableViewController *rootVC;
//邊欄控制器(自動(dòng)添加了手勢(shì) 自動(dòng)彈回功能)
@property(nonatomic,strong)UISplitViewController *spiltVC;
___________________________________________________________________________________________
#import "AppDelegate.h"
#import "RootTableViewController.h"
#import "DetailViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.rootVC = [[RootTableViewController alloc] initWithStyle:UITableViewStylePlain];
self.rootVC.navigationItem.title = @"導(dǎo)航視圖";
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:self.rootVC];
self.detailVC = [[DetailViewController alloc] init];
self.detailVC.navigationItem.title = @"詳情視圖";
UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:self.detailVC];
// 使用iPad專用控件實(shí)現(xiàn)分欄效果
self.spiltVC = [[UISplitViewController alloc] init];
//設(shè)置分欄試圖 先后順序決定了左右關(guān)系
self.spiltVC.viewControllers = @[rootNav,detailNav];
self.spiltVC.delegate = self.detailVC;
// 設(shè)置為窗口的跟視圖控制器
self.window.rootViewController = self.spiltVC;
return YES;
}
_________________________________________________________________________________________
RootTableViewController.m
#import "RootTableViewController.h"
#import "AppDelegate.h"
#import "DetailViewController.h"
@interface RootTableViewController ()
// 圖片名稱數(shù)組
@property(nonatomic,strong)NSArray *imgTitleArr;
// 圖片數(shù)組
@property(nonatomic,strong)NSArray *imgArr;
@end
@implementation RootTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imgTitleArr = @[@"圖片1",@"圖片2",@"圖片3",@"圖片4",@"圖片5",@"圖片6",@"圖片4"];
//通過循環(huán) 將圖片數(shù)組初始化
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (int i = 1; i <= 7 ; i++) {
NSString *imgName = [NSString stringWithFormat:@"car%d.jpg",i];
UIImage *img = [UIImage imageNamed:imgName];
[arr addObject:img];
}
//
self.imgArr = [arr copy];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.imgArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
cell.textLabel.text = self.imgTitleArr[indexPath.row];
;
return cell;
}
_________________________________________________________________________________________
拖拽xib
DetailViewController.m
#import "DetailViewController.h"#import "AppDelegate.h"#import "RootTableViewController.h"@interface DetailViewController ()@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"主菜單" style:UIBarButtonItemStylePlain target:self action:@selector(showPopOverController:)];
AppDelegate *app = [UIApplication sharedApplication].delegate;
app.spiltVC.displayModeButtonItem.title = @"顯示導(dǎo)航欄";
self.navigationItem.leftBarButtonItem =? app.spiltVC.displayModeButtonItem;
}
//回調(diào)方法
-(void)showPopOverController:(UIBarButtonItem *)sender
{
//? AppDelegate *app = [UIApplication sharedApplication].delegate;
RootTableViewController *rootVC = [[RootTableViewController 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(@"將要隱藏彈出視圖");
return YES;
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
NSLog(@"彈出視圖已經(jīng)隱藏");
}
#pragma mark - UISplitViewControllerDelegate
// 左側(cè)導(dǎo)航欄將要出現(xiàn)或隱藏時(shí)回調(diào)此方法
-(void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode
{
// 左側(cè)導(dǎo)航欄隱藏
if (displayMode == UISplitViewControllerDisplayModePrimaryHidden)
{
NSLog(@"左側(cè)導(dǎo)航將要隱藏");
AppDelegate *app = [UIApplication sharedApplication].delegate;
app.spiltVC.displayModeButtonItem.title = @"顯示導(dǎo)航欄";
//
svc.displayModeButtonItem.title = @"顯示導(dǎo)航欄";
self.navigationItem.leftBarButtonItem = app.spiltVC.displayModeButtonItem;
}
else if (displayMode == UISplitViewControllerDisplayModePrimaryOverlay)
{
NSLog(@"左側(cè)導(dǎo)航覆蓋到詳情視圖上");
}
else if (displayMode == UISplitViewControllerDisplayModeAllVisible)
{
NSLog(@"左側(cè)導(dǎo)航全部顯示");
self.navigationItem.leftBarButtonItem = nil;
}
else
{
NSLog(@"自動(dòng)顯示");
}
}