UISplitView with multiple ViewControllers in DetailView (storyboard)

僤鯓⒐⒋嵵緔 提交于 2019-12-25 02:20:14

问题


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:


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 ;)

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

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

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

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



来源:https://stackoverflow.com/questions/22462955/uisplitview-with-multiple-viewcontrollers-in-detailview-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!