Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

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

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

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...