Zhao Yi Zhao Yi - 1 year ago 56
Swift Question

How to show a segue inside a child view controller?

I have a main view controller, inside this view controller I show another view controller as its child view controller. The code to show the child view controller as shown below. The self.currentController is the child controller which will be located inside the main controller.

self.currentController?.view.frame = self.operationView.bounds

Now I want to perform a show segue (another controller, let call it ThirdController) inside the child view controller by using below code:

performSegueWithIdentifier("ShowSegue", sender: nil)

on doing this, the ThirdController will fill on the full screen. What I want to do is to show the third controller on the child controller place. How can I do this?


OK, Sorry I don't know how to write the answer in Swift. So I'll just show you how my solution is done in Objective-C.

Code to load the first subview:

- (void)loadASubview
    subview = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstView"];

    [self addChildViewController:subview];
    [self.view addSubview:subview.view];

    [subview didMoveToParentViewController:self];
    [subview.view setFrame:self.view.bounds];

Code to unload the subview:

- (void)unloadASubview
    [subview willMoveToParentViewController:nil];
    [subview.view removeFromSuperview];
    [subview removeFromParentViewController];

Initially, when I need to load subview A, I will simply call loadASubview. After that, if I need to load another subview, I will unload the subview I previously loaded by calling unloadASubview before loading the new subview.

Please take note that the "subview" variable inside the functions are declared outside.

I hope this will help you.