How to use multiple iOS custom view controllers without a navigation controller

前端 未结 2 1701
无人及你
无人及你 2020-12-29 13:49

I am building an app that uses multiple types of screens--all which warrant their own custom view controllers. I am successfully switching between view controllers and thei

相关标签:
2条回答
  • 2020-12-29 14:09

    One great way to do this is by using iOS5+'s UIViewController's ability to have child UIViewControllers (it's called view controller containment). I certainly had a difficult time figuring out how to do this until I watched the WWDC video that explains this in good detail.

    In a nutshell, it allows you to create your own parent view controller that owns a series of child view controllers. This single parent view controller can (and probably should, unless you're doing some really fancy stuff :P) sit as your app's window's root view controller. This method of having a single view controller act as a parent (and facilitate the adding, removing, and transitioning of the child view controllers) is reminiscent of what UINavigationController does (which is Apple's intent). Now you can create your own UINavigationController-like parent view controller, but have totally different transition animations and UI.

    As an example, in the parent view controller, in the viewDidLoad, I add the first child controller like this:

    self.currentlyDisplayedChildViewController = [[TheFirstViewController alloc] init];
    [self addChildViewController:self.currentlyDisplayedChildViewController];
    [self.view addSubview:self.currentlyDisplayedChildViewController.view];
    [self.currentlyDisplayedChildViewController didMoveToParentViewController:self];
    

    Then I would have a function to do the transition to the next child view controller (NOTE: this function belongs in the parent view controller -- the view controller that's going to act as your UINavigationController):

    - (void)transitionToViewController:(UIViewController *)nextChildViewController
    {
        [self addChildViewController:nextChildViewController];
        __weak TheParentViewController *me = self;
        [self transitionFromViewController:self.currentlyDisplayedChildViewController
                          toViewController:nextChildViewController
                                  duration:1.0f
                                   options:UIViewAnimationOptionTransitionFlipFromLeft
                                animations:nil
                                completion:^(BOOL finished)
                                {
                                    [nextChildViewController didMoveToParentViewController:self];
                                    [me.currentlyDisplayedChildViewController willMoveToParentViewController:nil];
                                    [me.currentlyDisplayedChildViewController removeFromParentViewController];
                                    me.currentlyDisplayedChildViewController = nextChildViewController;
                                }];
    }
    

    One thing really nice is you can use all the standard UIViewAnimationTransition options (or define your own custom animation in the animations block. Additionally, any orientation rotations events are automatically forwarded from the parent view controller to the child view controllers. This was one of that hairiest problems with doing custom root view controller manipulations on your own.

    I would suggest taking a look at WWDC2011 video titled "Implementing UIViewController Containment".

    0 讨论(0)
  • 2020-12-29 14:12

    Its not a bad solution. You basically set one view as the root view. When you need another UIViewController you set another one. Just be careful for the leaks...

    • Create the rootViewController as property of the class with retain.
    • Before this:

    rootViewController = (UIViewController *)[[BookViewController alloc] init];

    Add this:

    if(rootViewController){
        self.rootViewController=nil;
    }
    

    }

    So you release the previous one.

    Edit 1: One thing: my explanation here is based on the fact that you don't want to use an UINavigationController.

    0 讨论(0)
提交回复
热议问题