ipad与iphone兼容

1.效果预览图

1.iphone的运行效果

ipad与iphone兼容_第1张图片
Untitled2.gif

2.ipad的运行效果

ipad与iphone兼容_第2张图片
Untitled3.gif

2.注意点

注意适配
进行判断区分ipad与iphone

2.代码展示

AppDelegate.h

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

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

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

AppDelegate.m

#import "mainTableViewController.h"//主控制器
#import "firstViewController.h"
#import "secondViewController.h"
#import "thirdViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //主控制器
    mainTableViewController *main = [[mainTableViewController alloc]initWithStyle:UITableViewStylePlain];
    //导航栏
    UINavigationController *mainNav = [[UINavigationController alloc]initWithRootViewController:main];
    
    firstViewController *first = [[firstViewController alloc]init];
    secondViewController *second = [[secondViewController alloc]init];
    thirdViewController *third = [[thirdViewController alloc]init];
    
    //判断如果是ipad运行
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
        //把第一控制器添加到导航栏
        UINavigationController *firstNav = [[UINavigationController alloc]initWithRootViewController:first];
        NSLog(@"----->%p",firstNav);
        
        
        //把第二控制器添加到导航栏
        UINavigationController *secondNav = [[UINavigationController alloc]initWithRootViewController:second];
        
        
        //把第三控制器添加到导航栏
        UINavigationController *thirdNav = [[UINavigationController alloc]initWithRootViewController:third];
        
        //添加到存储iPad运行时,右侧详情导航视图控制器的数组中
        self.detailNavArr = @[firstNav,secondNav,thirdNav];
        
        //初始化左侧的导航栏控制器
        self.splitVc = [[UISplitViewController alloc]init];
        self.splitVc.viewControllers = @[mainNav,firstNav];
        
        self.window.rootViewController = self.splitVc;
        
        
    }
    else
    {
        self.detailArr = @[first,second,third];
        self.window.rootViewController = mainNav;
    }
    return YES;
}

注意继承UITableViewController

mainTableViewController.m

#import "AppDelegate.h"
@interface mainTableViewController ()

//
@property(nonatomic,strong)NSArray *titleArr;
@end

@implementation mainTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //显示cell的数据
    self.titleArr = @[@"运动",@"菜系",@"游戏"];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

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

    return 1;
}

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

    return self.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 *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    //如果是ipad运行
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
        app.splitVc.viewControllers = @[self.navigationController,app.detailNavArr[indexPath.row]];
    }else
    {
        NSLog(@"%@",app.detailNavArr[indexPath.row]);
        [self.navigationController pushViewController:app.detailArr[indexPath.row] animated:NO];
    }

xiangqingViewController.m


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gotoxinagqing)];
    tap.numberOfTapsRequired = 2;
    [self.view addGestureRecognizer:tap];
    
}
-(void)gotoxinagqing
{
    xiangqingViewController *xiangqing = [[xiangqingViewController alloc]init];
    [self.navigationController pushViewController:xiangqing animated:NO];
    NSLog(@"详情控制器----->%p",self.navigationController);
}

你可能感兴趣的:(ipad与iphone兼容)