UISplitView在DetailView中有多个ViewController(故事板) [英] UISplitView with multiple ViewControllers in DetailView (storyboard)

查看:116
本文介绍了UISplitView在DetailView中有多个ViewController(故事板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种解决方案,以使UISplitView的DetailView(UISplitView的rightView)内部具有多个ViewController.

i'am looking for a solution to have a UISplitView with multiple ViewControllers inside the DetailView(rightView of the UISplitView).

以苹果为例,但使用nib文件而不是情节提要. ( https://developer.apple.com/library/ios/samplecode/multipledetailviews/Listings/ReadMe_txt.html )

The example of apple works fine but uses nib file instead of storyboards. (https://developer.apple.com/library/ios/samplecode/multipledetailviews/Listings/ReadMe_txt.html)

我找到了另一个示例,但是当我实现UITableView时存在空格 http://www.dharmaworks.net/Consulting/switching -detail-views-in-uisplitviewcontroller-with-ios7

I found another example but there is whitespace when i implement an UITableView http://www.dharmaworks.net/Consulting/switching-detail-views-in-uisplitviewcontroller-with-ios7

推荐答案

在寻找答案的过程中,我发现很多人都遇到同样的问题.我自己弄清楚了,所以这是我的解决方案.我希望它对其他人有用.

During the search for an answer i found many people with the same issue. I figured it out myself so here is my solution. I hope it is useful for other people.

步骤1 .创建一个SplitViewController项目.如果您已经有一个项目,请跳过此步骤;)

Step 1. Create a SplitViewController project. If you have a project already skip this step ;)

第2步. 添加两个不同的viewControllers.在这种情况下,我称它们为AbcViewController和XyzViewController.

Step 2. Add two different viewControllers. In this case i call them AbcViewController and XyzViewController.

第3步. 转到ipad情节提要,从情节提要中删除DetailViewController.然后添加两个新的viewControllers.

Step 3. Go to the ipad storyboard, remove the DetailViewController from the storyboard. Then add two new viewControllers.

第4步. 为您的viewControllers设置类和Storyboard ID.

Step 4. Set the class and the Storyboard ID for your viewControllers.

第5步. 转到您的MasterViewController.h,然后将代码替换为下面的代码.

Step 5. Go to your MasterViewController.h and replace the code with the code below.

#import <UIKit/UIKit.h>

@class AbcViewController;
@class XyzViewController;

@interface MasterViewController : UITableViewController

@property (strong, nonatomic) AbcViewController *abcViewController;
@property (strong, nonatomic) XyzViewController *xyzViewController;

@end

第6步. 现在转到您的MasterViewController.m文件,并替换为以下代码:

Step 6. Now go to your MasterViewController.m file and replace with this code:

注意::如果您已有项目,并且不想替换,请使用步骤7中的代码.

Note: If you have an existing project and don't want to replace use the code in step 7.

#import "MasterViewController.h"
#import "DetailViewController.h"

@interface MasterViewController () {
    NSMutableArray *_objects;
}
@end

@implementation MasterViewController

- (void)awakeFromNib
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.clearsSelectionOnViewWillAppear = NO;
        self.preferredContentSize = CGSizeMake(320.0, 600.0);
    }
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.detailViewController = (DetailViewController*)[[self.splitViewController.viewControllers lastObject] topViewController];
}

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

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    if (indexPath.row == 0) {
        cell.textLabel.text = @"ABC";
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"XYZ";
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.abcViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ABC"];
    self.xyzViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"XYZ"];
    if (indexPath.row == 0) {
        NSArray *newVCs = [NSArray arrayWithObjects:[[[self splitViewController ] viewControllers ] firstObject ] , self.abcViewController, nil];
        self.splitViewController.viewControllers = newVCs;
    }
    if (indexPath.row == 1) {
        NSArray *newVCs = [NSArray arrayWithObjects:[[[self splitViewController ] viewControllers ] firstObject ] , self.xyzViewController, nil];
        self.splitViewController.viewControllers = newVCs;
    }
}

@end

第7步.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        self.abcViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ABC"];
        self.xyzViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"XYZ"];

        if (indexPath.row == 0) {
            NSArray *newVCs = [NSArray arrayWithObjects:[[[self splitViewController ] viewControllers ] firstObject ] , self.abcViewController, nil];
            self.splitViewController.viewControllers = newVCs;
        }
        if (indexPath.row == 1) {
            NSArray *newVCs = [NSArray arrayWithObjects:[[[self splitViewController ] viewControllers ] firstObject ] , self.xyzViewController, nil];
            self.splitViewController.viewControllers = newVCs;
        }
    }


就这样,运行您的项目并享受:)


Thats it, run your project and enjoy :)

这篇关于UISplitView在DetailView中有多个ViewController(故事板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆