Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I switch between two child View Controllers on Swift?

I am trying to switch between two child View Controllers on a particular Container View. I have a Navigation Controller with a menu (Table View to make the different options of the menu).

Each time I click an option of the menu I would like to change the child of the Container View but I am getting the child above Navigation bar and Table View (they are not shown but they are under the new child View Controller).

The scheme of my Main.storyboard is like this:

Navigation Controller --> View Controller (With 2 Container View, 1 for Table View
                                           and the other to set master View Controller)
                                         |
                                         |
                               ------------------------
                               |                      |
                          View Controller        Table View
                            (id: master)

                   View Controller (id: Home)   View Controller (id: screen2)

I have the following code on tableView function (in which I detect when an option of the menu is clicked) to change the child View Controller of the Container View:

let currentController = self.navigationController?.visibleViewController //container

var oldChild = (currentController?.childViewControllers.last!)! as UIViewController //master
let newChild = (storyboard?.instantiateViewControllerWithIdentifier("Home"))! as UIViewController //Home

if (oldChild != newChild){
    if currentController.childViewControllers.last != nil{
        oldChild.willMoveToParentViewController(nil)
        currentController.navigationController?.navigationBar.willRemoveSubview(oldChild.view)
        //oldChild.view.removeFromSuperview()
        oldChild.removeFromParentViewController()
    }

    currentController.addChildViewController(newChild)
    currentController.view.addSubview(newChild.view)
    newChild.didMoveToParentViewController(currentController) 
}

This code is working almost well. The problem is that the new child View Controller is being shown above the Navigation bar and the Table View (the menu). So it is occupying the full screen instead of fit on the Container View.

Should I add something more to my code or am I using my code in a wrong way? I have searched a lot about it but most of the solutions are in objective-c or does not work for me.

EDIT: After searching for a lot of hours I suspect that it is something related with the embeded segue that connects the root View Controller with master View Controller but I am not able to embed the new child to the Container View. The code that I am trying is:

currentController.performSegueWithIdentifier("EmbedSegue", sender: newChild)

or

currentController.presentViewController(newChild, animated: true, completion: nil)

but none of them embed the segue. Just display the newChild on the full screen.

Thanks in advance!

like image 598
Francisco Romero Avatar asked Sep 05 '16 10:09

Francisco Romero


People also ask

How do I switch between view controllers?

Right-click the control or object in your current view controller. Drag the cursor to the view controller you want to present. Select the kind of segue you want from the list that Xcode provides.


1 Answers

You should have the IBOutlet for ContainerView and addSubview to this IBOutlet

currentController.containerView.addSubview(newChild.view)

or

currentController.addSubview(childView.view, toView: self.containerView)
like image 192
Huy Vu Avatar answered Oct 05 '22 03:10

Huy Vu