iOS ipad和iphone兼容~demo

作者: 石虎132 | 来源:发表于2017-08-09 00:14 被阅读160次

    //联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄

    /**

    注意点: 1.看 GIF 效果图.

    2.看连线视图的效果图.

    3.看实现代码(直接复制实现效果).

    */

    一、GIF 效果图:

    图1~iphone:

    图2~ipad:

    二、连线视图的效果图:

    图1:

    图2:

    图3:

    图4:

    图5:

    三、实现代码:

    =========================

    ===================================================

    ====================

    控制器1:AppDelegate.h

    //  AppDelegate.h

    //  ipad和iphone兼容dome

    //

    //  Created by石虎on 2017/8/8.

    //  Copyright © 2017年shihu. All rights reserved.

    //

    #import

    @interfaceAppDelegate :UIResponder

    @property(strong,nonatomic)UIWindow*window;

    @property(nonatomic,strong)UISplitViewController*splitVC;//左侧的导航栏控制器

    @property(nonatomic,strong)NSArray*detailNavArr;//存储iPad运行时,右侧详情导航视图控制器的数组

    @property(nonatomic,strong)NSArray*detailVCArr;//存储iPhone运行时,右侧需要进栈的视图控制器数组

    @end

    ==========

    ======

    ====

    控制器1:AppDelegate.m

    //  AppDelegate.m

    //  ipad和iphone兼容dome

    //

    //  Created by石虎on 2017/8/8.

    //  Copyright © 2017年shihu. All rights reserved.

    //

    #import"AppDelegate.h"

    #import"SHMasterTableViewController.h"//主控制器

    #import"SHFirstViewController.h"

    #import"SHSecondViewController.h"

    #import"SHThirdViewController.h"

    @interfaceAppDelegate()

    @end

    @implementationAppDelegate

    - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

    //主控制器

    SHMasterTableViewController*masterVC = [[SHMasterTableViewControlleralloc]initWithStyle:UITableViewStylePlain];

    //导航栏

    UINavigationController*masterNav = [[UINavigationControlleralloc]initWithRootViewController:masterVC];

    SHFirstViewController*firstVC = [[SHFirstViewControlleralloc]init];

    SHSecondViewController*secVC = [[SHSecondViewControlleralloc]init];

    SHThirdViewController*thirdVC = [[SHThirdViewControlleralloc]init];

    //判断如果是iPad运行

    if([UIDevicecurrentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad)

    {

    //把第一控制器添加到导航栏

    UINavigationController*firstNav = [[UINavigationControlleralloc]initWithRootViewController:firstVC];

    NSLog(@"----->>> %p",firstNav);

    //把第二控制器添加到导航栏

    UINavigationController*secNav = [[UINavigationControlleralloc]initWithRootViewController:secVC];

    //把第三控制器添加到导航栏

    UINavigationController*thirdNav = [[UINavigationControlleralloc]initWithRootViewController:thirdVC];

    //添加到存储iPad运行时,右侧详情导航视图控制器的数组中

    self.detailNavArr=@[firstNav,secNav,thirdNav];

    //初始化左侧的导航栏控制器

    self.splitVC= [[UISplitViewControlleralloc]init];

    self.splitVC.viewControllers=@[masterNav,firstNav];

    self.window.rootViewController=self.splitVC;

    }

    #pragma mark - iPhone运行

    else

    {

    self.detailVCArr=@[firstVC,secVC,thirdVC];

    self.window.rootViewController= masterNav;

    }

    returnYES;

    }

    =========================

    ===================================================

    ====================

    控制器2:SHMasterTableViewController.m

    //  SHMasterTableViewController.m

    //  ipad和iphone兼容dome

    //

    //  Created by石虎on 2017/8/8.

    //  Copyright © 2017年shihu. All rights reserved.

    //

    #import"SHMasterTableViewController.h"

    #import"AppDelegate.h"

    @interfaceSHMasterTableViewController()

    //

    @property(nonatomic,strong)NSArray*titleArr;

    @end

    @implementationSHMasterTableViewController

    - (void)viewDidLoad {

    [superviewDidLoad];

    //显示cell上的数据

    self.titleArr=@[@"运动",@"菜系",@"游戏"];

    }

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {

    return1;

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    returnself.titleArr.count;

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    staticNSString *identifier =@"CELL";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    //缓存池

    if(cell ==nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }

    cell.textLabel.text =self.titleArr[indexPath.row];

    returncell;

    }

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {

    AppDelegate *appDele = (AppDelegate *)[UIApplication sharedApplication].delegate;

    //如果是iPad运行

    if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)

    {

    appDele.splitVC.viewControllers =@[self.navigationController,appDele.detailNavArr[indexPath.row]];

    }else{// iPhone运行

    [self.navigationController pushViewController:appDele.detailVCArr[indexPath.row] animated:YES];

    }

    }

    @end

    =========================

    ===================================================

    ====================

    控制器3:SHFirstViewController.m

    //

    //  Created by石虎on 2017/8/8.

    //  Copyright © 2017年shihu. All rights reserved.

    //

    #import"SHFirstViewController.h"

    #import"SHDetailViewController.h"//详情控制器

    @interfaceSHFirstViewController()

    @end

    @implementationSHFirstViewController

    - (void)viewDidLoad {

    [superviewDidLoad];

    //手势

    UITapGestureRecognizer*tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(gotoDetailVC)];

    tap.numberOfTapsRequired=2;

    [self.viewaddGestureRecognizer:tap];

    }

    //回调手势

    -(void)gotoDetailVC

    {

    SHDetailViewController*detailVC = [[SHDetailViewControlleralloc]init];

    [self.navigationControllerpushViewController:detailVCanimated:YES];

    NSLog(@"详情控制器----->%p",self.navigationController);

    }

    @end

    =========================

    ===================================================

    ====================

    控制器4:SHDetailViewController.m

    //  Created by石虎on 2017/8/8.

    //  Copyright © 2017年shihu. All rights reserved.

    //

    #import"SHDetailViewController.h"

    @interfaceSHDetailViewController()

    @end

    @implementationSHDetailViewController

    - (void)viewDidLoad {

    [superviewDidLoad];

    NSLog(@"我是第详情大控制器");

    }

    @end

    =========================

    ===================================================

    ====================

    控制器5:SHSecondViewController.m

    //  Created by石虎on 2017/8/8.

    //  Copyright © 2017年shihu. All rights reserved.

    //

    #import"SHSecondViewController.h"

    @interfaceSHSecondViewController()

    @end

    @implementationSHSecondViewController

    - (void)viewDidLoad {

    [superviewDidLoad];

    NSLog(@"我是第二大控制器");

    }

    @end

    =========================

    ===================================================

    ====================

    控制器6:SHThirdViewController.m

    //  Created by石虎on 2017/8/8.

    //  Copyright © 2017年shihu. All rights reserved.

    //

    #import"SHThirdViewController.h"

    @interfaceSHThirdViewController()

    @end

    @implementationSHThirdViewController

    - (void)viewDidLoad {

    [superviewDidLoad];

    NSLog(@"我是第三大控制器");

    }

    @end

    =========================

    ===================================================

    ====================

    谢谢!!!

    相关文章

      网友评论

        本文标题:iOS ipad和iphone兼容~demo

        本文链接:https://www.haomeiwen.com/subject/lyzxrxtx.html