Oc iPad開發~dome

20170807192304754.gif

控制器1:AppDelegate.h

#import <UIKit/UIKit.h>

@class SHDetailViewController,SHRootTableViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@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"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.rootVC = [[SHRootTableViewController alloc] initWithStyle:UITableViewStylePlain];
    self.rootVC.navigationItem.title = @"導航視圖";
    UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:self.rootVC];


    self.detailVC = [[SHDetailViewController alloc] init];
    self.detailVC.navigationItem.title = @"詳情視圖";
    UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:self.detailVC];



    // 使用iPad專用控件實現分欄效果
    self.spiltVC = [[UISplitViewController alloc] init];
    //設置分欄試圖 先後順序決定了左右關係
    self.spiltVC.viewControllers = @[rootNav,detailNav];

    //self.spiltVC.delegate = self.detailVC;

    // 設置爲窗口的跟視圖控制器
    self.window.rootViewController = self.spiltVC;

    return YES;
}

控制器2:

#import "SHRootTableViewController.h"//左側邊欄的表格界面
#import "SHDetailViewController.h"
#import "AppDelegate.h"

@interface SHRootTableViewController ()

// 圖片名稱數組
@property(nonatomic,strong)NSArray *imgTitleArr;
// 圖片數組
@property(nonatomic,strong)NSArray *imgArr;
@end

@implementation SHRootTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.imgTitleArr = @[@"大衆",@"法拉利",@"寶馬",@"奔馳",@"JEEP",@"邁巴赫",@"蘭博基尼"];

    //通過循環 將圖片數組初始化
    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;
}

//跳轉
-(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 <UIKit/UIKit.h>

@interface SHDetailViewController : UIViewController
//顯示圖片
@property (strong, nonatomic) IBOutlet UIImageView *imgView;
@end

控制器3: SHDetailViewController.m

#import "SHDetailViewController.h"
#import "AppDelegate.h"
#import "SHRootTableViewController.h"//左側邊欄的表格界面

@interface SHDetailViewController ()<UISplitViewControllerDelegate,UIPopoverControllerDelegate>

@end

@implementation SHDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //導航欄
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"主菜單" style:UIBarButtonItemStylePlain target:self action:@selector(showPopOverController:)];

    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].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(@"將要隱藏彈出視圖");
    return YES;
}

- (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;

    }
    else if (displayMode == UISplitViewControllerDisplayModePrimaryOverlay)
    {
        NSLog(@"左側導航覆蓋到詳情視圖上");
    }
    else if (displayMode == UISplitViewControllerDisplayModeAllVisible)
    {
        NSLog(@"左側導航全部顯示");
        self.navigationItem.leftBarButtonItem = nil;
    }
    else
    {
        NSLog(@"自動顯示");
    }
}
@end
發佈了57 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章