Oc iPad与iPhone兼容~demo

本文展示了如何使iOS应用在iPad和iPhone上兼容。通过一系列控制器的实现,包括AppDelegate.h/m, SHMasterTableViewController.m, SHFirstViewController.m, SHDetailViewController.m, SHSecondViewController.m和SHThirdViewController.m,确保了在不同设备上的良好用户体验。" 127458843,15230146,Linux进程与计划任务管理详解,"['Linux', '运维', '服务器']

屏幕快照 2017-08-09 20.49.01.png
图1~iphone:
20170808235344952.gif
图2~ipad:
20170808235405103.gif

20170809000454516.png

20170809000532851.png

20170809000632513.png

20170809000656249.png
实现代码:
控制器1:AppDelegate.h

#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

#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

#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

#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

#import "SHDetailViewController.h"

@interface SHDetailViewController ()

@end

@implementation SHDetailViewController

- (void)viewDidLoad {
    [superviewDidLoad];
    NSLog(@"我是第  详情  大控制器"); 
}
@end

控制器5: SHSecondViewController.m

#import "SHSecondViewController.h"

@interface SHSecondViewController ()

@end

@implementation SHSecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

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

控制器6: SHThirdViewController.m

#import "SHThirdViewController.h"

@interface SHThirdViewController ()

@end

@implementation SHThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值