UISplitView with multiple ViewControllers in Detai

2019-09-05 22:50发布

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

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)

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

1条回答
三岁会撩人
2楼-- · 2019-09-05 23:42

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.

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

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

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

enter image description here

Step 4. Set the class and the Storyboard ID for your viewControllers. enter image description here

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

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

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

Step 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 :)

查看更多
登录 后发表回答