我认为一种方法是使用容器视图,其中包含容器视图控制器。该容器控制器将有 2 个子控制器,它们是您的 CoreDateTableViewController 和 NRGridViewController。我已经实现了类似的东西,如果你有兴趣,我可以给你看一些代码。
编辑后:在一个测试应用程序中,我从一个视图模板和一个故事板开始。我在视图的顶部添加了两个按钮,在视图的下半部分添加了一个容器视图(第一个控制器属于 ViewController 类)。然后我拖出一个新的视图控制器,并将控件从容器视图拖到新控制器并选择“嵌入segue”(这会将视图调整为与容器视图相同的大小)。该控制器的类已更改为我的子类 ContainerController。然后,我为将由容器控制器管理的 2 个视图创建了另外 2 个控制器(视图需要在 IB 中将其大小设置为“自由格式”,以便您可以将大小设置为与容器视图相同)。下面是 ContainerController 中的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.cont1 = [[FirstController alloc]initWithNibName:@"FirstView" bundle:nil];
self.cont2 = [[SecondController alloc]initWithNibName:@"SecondController" bundle:nil];
[self addChildViewController:self.cont1];
self.currentController = self.cont1;
[self.view addSubview:self.cont1.view];
}
-(void)switchToFirst {
if (self.currentController != self.cont1) {
[self addChildViewController:self.cont1];
[self moveToNewController:self.cont1];
}
}
-(void)switchToSecond {
if (self.currentController != self.cont2) {
[self addChildViewController:self.cont2];
[self moveToNewController:self.cont2];
}
}
-(void)moveToNewController:(id) newController {
[self.currentController willMoveToParentViewController:nil];
[self transitionFromViewController:self.currentController toViewController:newController duration:.6 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{}
completion:^(BOOL finished) {
[self.currentController removeFromParentViewController];
[newController didMoveToParentViewController:self];
self.currentController = newController;
}];
}
我在 ViewController 中拥有的唯一代码是用于切换视图的 2 个按钮的 IBActions。这些方法只是调用容器控制器中的方法:
-(IBAction)chooseFirstController:(id)sender {
[self.childViewControllers.lastObject switchToFirst];
}
-(IBAction)chooseSecondController:(id)sender {
[self.childViewControllers.lastObject switchToSecond];
}