What really is the best way to have MORE THAN ONE controller in a container?

夙愿已清 提交于 2019-11-27 08:47:26

问题


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

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

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???

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

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

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


回答1:


You have - as expected - several options.

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.

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

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

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.


Footnote for beginners:

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



回答2:


I used a segmented control with a containing scrollView.

The container view and segmented control are a subviews of the scrollView. When I press one of the tabs in the segmented control, I use the two method of removing a sub view controller, showing new subview controller, then I recalculate the height required for the newly loaded sub view controller and set the scrollView's autolayout height constraint constant to the new value.



来源:https://stackoverflow.com/questions/25550678/what-really-is-the-best-way-to-have-more-than-one-controller-in-a-container

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