在容器中拥有多个控制器的最佳方式是什么? [英] What really is the best way to have MORE THAN ONE controller in a container?

查看:17
本文介绍了在容器中拥有多个控制器的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 ViewController,它有一个容器视图.请注意,容器只是全屏的一部分,与往常一样.

Here's a ViewController, and it has a container view. Note the container is only a part of the fullscreen, as usual.

因此容器包含右侧的 VC.例如,它可以是显示部件列表的部件".

So the container contains the VC on the right. It could be, say, "Parts" showing the Parts list.

但是如果我说四个 VC,我想放在那个区域(容器所在的位置)呢?可能:零件、轮胎、制动器、机油.

But what if I have say four VCs, I want to put in that area (where the container is). Maybe: Parts, Tires, Brakes, Oils.

当然,在那个区域一次只会显示一个.

Of course, only one will show at a time, on that area.

真正做到这一点的最佳方式是什么?

What the heck is the best way to really do that?

过去,我制作了四个容器视图(在完全相同的位置和大小),然后只调出我想要的一个,然后手动删除了其他三个.

In the past I've made four container views (in the exact same location and size) and just brought up the one I wanted, and manually removed the other three.

这里的正确解决方案是什么???

What is the proper solution here???

注意——一个相关的问题是,确实,容器视图实际上可以指向多个 VC ??(那样你仍然可以手动"交换,只需要一个而不是五个匹配的容器视图会容易得多.)

Note -- a related question is, indeed, can a container view in fact point to more than one VC ?? (In that way you could still swap "manually", it would just be much easier to need only one, not five matching, container views.)

最后这里是在 www 上找到的相关文章...

Finally here's a related essay found on the www ...

http://sandmoose.com/post/35714028270/storyboards-with-custom-container-view-controllers

推荐答案

正如预期的那样,您有多种选择.

You have - as expected - several options.

选项 1使用 UIPageViewController.然后你甚至可以在不同的子视图控制器之间滑动,它们只会在需要时加载.

Option 1 Use a UIPageViewController. You can then even swipe between the different child view controllers and they will only be loaded when they are needed.

您必须将UIPageViewControllerdataSource 设置为至少实现这两个方法的对象:

You have to set the UIPageViewController's dataSource to an object that implements at least these two methods:

#pragma mark - UIPageViewControllerDataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
      viewControllerBeforeViewController:(UIViewController *)viewController
{
    // Return the viewController instance _before_ the given viewController or nil when there are no more view controllers to display.

    return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
       viewControllerAfterViewController:(UIViewController *)viewController
{
    // Return the viewController instance _after_ the given viewController or nil when there are no more view controllers to display.

    return nil;
} 

选项 2为您的容器视图创建一个出口,然后以编程方式添加/删除您要显示的子视图控制器,如下所示:

Option 2 Create an outlet for your container view and then programmatically add/remove the child view controller you want to display, like so:

- (void)setCurrentChildViewController:(UIViewController *)viewController
{
    // Remove existing child
    if (self.currentChildViewController) {
        if (self.currentChildViewController.isViewLoaded) {
            [self.currentChildViewController.view removeFromSuperview];
        }
        [self.currentChildViewController willMoveToParentViewController:nil];
        [self.currentChildViewController removeFromParentViewController];
    }

    // Now add viewController as child
    [self addChildViewController:viewController];
    [viewController didMoveToParentViewController:self];
    viewController.view.frame = self.containerView.bounds;
    viewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    [self beginAppearanceTransition:YES animated:NO];
    [self.containerView addSubview:viewController.view];
    [self endAppearanceTransition];

    self.currentChildViewController = viewController;
}

选项 3按照您在问题中的描述隐藏和显示子视图控制器,但我更愿意根据您的需要选择选项 1 或 2.

Option 3 Hide and show the child view controllers as you have described in your question, but I'd rather pick option 1 or 2, depending on your needs.

初学者脚注:

使用Storyboards,当你加载一个UIViewController时,你经常需要使用instantiateViewControllerWithIdentifier:,所以,一个简单的例子

With Storyboards, when you load a UIViewController, you often need to use instantiateViewControllerWithIdentifier:, so, a simple example

SomeViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"someViewControllerStoryboardID"];
// see method created in option 2
[self setCurrentChildViewController:vc];

这篇关于在容器中拥有多个控制器的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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