Oc iPad与iPhone兼容~demo

来源:互联网 发布:算法c语言实现高清pdf 编辑:程序博客网 时间:2024/05/22 00:46

屏幕快照 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
原创粉丝点击