在寻找答案的过程中,我发现很多人都有同样的问题。我自己想出来了,所以这是我的解决方案。我希望它对其他人有用。
步骤 1.创建一个 SplitViewController 项目。如果你有一个项目已经跳过这一步;)

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

步骤 3.
转到 ipad 情节提要,从情节提要中删除 DetailViewController。然后添加两个新的视图控制器。

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

步骤 5.
转到您的 MasterViewController.h 并将代码替换为以下代码。
#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 文件并替换为以下代码:
注意:如果您有一个现有项目并且不想替换,请使用第 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;
}
}
就是这样,运行您的项目并享受:)