Oc iPad开发~dome

本文介绍了一种在iOS应用中实现分屏视图的方法,包括左侧边栏表格视图和右侧详情视图的交互逻辑。左侧边栏展示了一系列汽车品牌的图片和名称,点击后会在右侧视图显示对应的图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

20170807192304754.gif

控制器1:AppDelegate.h

#import <UIKit/UIKit.h>

@class SHDetailViewController,SHRootTableViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

//详情界面
@property(nonatomic,strong)SHDetailViewController *detailVC;
//左侧边栏的表格界面
@property(nonatomic,strong)SHRootTableViewController *rootVC;
//边栏控制器(自动添加了手势 自动弹回功能)
@property(nonatomic,strong)UISplitViewController *spiltVC;

@end

控制器1: AppDelegate.m

#import "AppDelegate.h"
#import "SHRootTableViewController.h"
#import "SHDetailViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.rootVC = [[SHRootTableViewController alloc] initWithStyle:UITableViewStylePlain];
    self.rootVC.navigationItem.title = @"导航视图";
    UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:self.rootVC];


    self.detailVC = [[SHDetailViewController alloc] init];
    self.detailVC.navigationItem.title = @"详情视图";
    UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:self.detailVC];



    // 使用iPad专用控件实现分栏效果
    self.spiltVC = [[UISplitViewController alloc] init];
    //设置分栏试图 先后顺序决定了左右关系
    self.spiltVC.viewControllers = @[rootNav,detailNav];

    //self.spiltVC.delegate = self.detailVC;

    // 设置为窗口的跟视图控制器
    self.window.rootViewController = self.spiltVC;

    return YES;
}

控制器2:

#import "SHRootTableViewController.h"//左侧边栏的表格界面
#import "SHDetailViewController.h"
#import "AppDelegate.h"

@interface SHRootTableViewController ()

// 图片名称数组
@property(nonatomic,strong)NSArray *imgTitleArr;
// 图片数组
@property(nonatomic,strong)NSArray *imgArr;
@end

@implementation SHRootTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.imgTitleArr = @[@"大众",@"法拉利",@"宝马",@"奔驰",@"JEEP",@"迈巴赫",@"兰博基尼"];

    //通过循环 将图片数组初始化
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (int i = 1; i <= 7 ; i++) {
        NSString *imgName = [NSString stringWithFormat:@"car%d.jpg",i];
        UIImage *img = [UIImage imageNamed:imgName];
        [arr addObject:img];
    }
    //
    self.imgArr = [arr copy];

}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

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

    return self.imgArr.count;
}

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

    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //缓存池
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    cell.textLabel.text = self.imgTitleArr[indexPath.row];

    return cell;
}

//跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 得到在Appdele中实例化的DetailViewController对象的内存
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    SHDetailViewController *detaiVC = appDelegate.detailVC;
    detaiVC.imgView.image = self.imgArr[indexPath.row];
}

@end

控制器3: SHDetailViewController.h

#import <UIKit/UIKit.h>

@interface SHDetailViewController : UIViewController
//显示图片
@property (strong, nonatomic) IBOutlet UIImageView *imgView;
@end

控制器3: SHDetailViewController.m

#import "SHDetailViewController.h"
#import "AppDelegate.h"
#import "SHRootTableViewController.h"//左侧边栏的表格界面

@interface SHDetailViewController ()<UISplitViewControllerDelegate,UIPopoverControllerDelegate>

@end

@implementation SHDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //导航栏
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"主菜单" style:UIBarButtonItemStylePlain target:self action:@selector(showPopOverController:)];

    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    app.spiltVC.displayModeButtonItem.title = @"显示导航栏";
    self.navigationItem.leftBarButtonItem =  app.spiltVC.displayModeButtonItem;
}
//回调方法
-(void)showPopOverController:(UIBarButtonItem *)sender
{
    //左侧边栏的表格界面
    SHRootTableViewController *rootVC = [[SHRootTableViewController alloc] initWithStyle:UITableViewStylePlain];

    // 弹出视图
    UIPopoverController *popCtl = [[UIPopoverController alloc] initWithContentViewController:rootVC];
    // 弹出视图大小
    popCtl.popoverContentSize = CGSizeMake(200, 300);
    popCtl.backgroundColor = [UIColor yellowColor];
    popCtl.delegate = self;

    // 弹出该视图
    [popCtl presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}


#pragma mark - UIPopoverControllerDelegate
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
    NSLog(@"将要隐藏弹出视图");
    return YES;
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    NSLog(@"弹出视图已经隐藏");
}
#pragma mark - UISplitViewControllerDelegate

// 左侧导航栏将要出现或隐藏时回调此方法
-(void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode
{
    // 左侧导航栏隐藏
    if (displayMode == UISplitViewControllerDisplayModePrimaryHidden)
    {
        NSLog(@"左侧导航将要隐藏");
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        app.spiltVC.displayModeButtonItem.title = @"显示导航栏";
        //
        svc.displayModeButtonItem.title = @"显示导航栏";

        self.navigationItem.leftBarButtonItem = app.spiltVC.displayModeButtonItem;

    }
    else if (displayMode == UISplitViewControllerDisplayModePrimaryOverlay)
    {
        NSLog(@"左侧导航覆盖到详情视图上");
    }
    else if (displayMode == UISplitViewControllerDisplayModeAllVisible)
    {
        NSLog(@"左侧导航全部显示");
        self.navigationItem.leftBarButtonItem = nil;
    }
    else
    {
        NSLog(@"自动显示");
    }
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值