iOS ipad和iphone兼容~demo

来源:互联网 发布:2017php面试题大全 编辑:程序博客网 时间:2024/05/18 02:33

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

/**

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

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

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

*/

一、GIF 效果图:

图1~iphone:



图2~ipad:



二、连线视图的效果图:

图1:



图2:



图3:



图4:



图5:



三、实现代码:

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

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

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

控制器1:AppDelegate.h

//  AppDelegate.h

//  ipadiphone兼容dome

//

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

//  Copyright © 2017 shihu. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface AppDelegate :UIResponder <UIApplicationDelegate>


@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

//  ipadiphone兼容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"


@interface AppDelegate ()

@end

@implementation AppDelegate


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

//  ipadiphone兼容dome

//

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

//  Copyright © 2017 shihu. All rights reserved.

//


#import "SHMasterTableViewController.h"

#import "AppDelegate.h"


@interface SHMasterTableViewController ()

//

@property(nonatomic,strong)NSArray *titleArr;

@end


@implementation SHMasterTableViewController


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

    

    static NSString *identifier =@"CELL";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    //缓存池

    if (cell ==nil) {

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

    }

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


    return cell;

}


-(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"//详情控制器


@interface SHFirstViewController ()


@end


@implementation SHFirstViewController


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


@interface SHDetailViewController ()


@end


@implementation SHDetailViewController


- (void)viewDidLoad {

    [superviewDidLoad];

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

}

@end


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

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

====================
控制器5: SHSecondViewController.m


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

//  Copyright © 2017 shihu. All rights reserved.

//


#import "SHSecondViewController.h"


@interface SHSecondViewController ()


@end


@implementation SHSecondViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

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

}

@end


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

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

====================
控制器6: SHThirdViewController.m

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

//  Copyright © 2017 shihu. All rights reserved.

//


#import "SHThirdViewController.h"


@interface SHThirdViewController ()


@end


@implementation SHThirdViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

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

}

@end



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

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

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



谢谢!!!


阅读全文
1 0
原创粉丝点击