Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode - How can i programmatically embed/change view controller within a container view?

I am trying to programmatically set / or change the embedded View Controller inside a Controller View.

I have 3 different View Controllers, that I would like to show in the Container View, all depending on if the user is logged in or not.

I have looked around and tried a bunch of code, I found one that worked, but the code changed the self view, and not the view containers view.

A lot of the code I have tried have also not been in Swift 3, so as a new app developer, this has been quite stressful, as I tried to convert it to Swift 3.

Can anyone provide a solution for changing the embedded view controller inside a view container? Thanks.

like image 973
JDoe Avatar asked Jul 15 '17 12:07

JDoe


2 Answers

I might have found a solution for this. I'm answering here, in case it can help anyone else in my situation.

What I did was add a new View Controller and then embed it to the View Container - This will work as a "master view" - I then use this blank view controller to decide which other view controller should be changed within the self of the blank.

Here's some code I have in the blank view controller, but I suppose the blank view controller can also be used as a master view controller (in my case for "Account"), and then it can add the login/register view as a child.

override func viewDidLoad() {
    super.viewDidLoad()

    updateView()
}

private lazy var loginViewController: loginViewController = {
    // Load Storyboard
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

    // Instantiate View Controller
    var viewController = storyboard.instantiateViewController(withIdentifier: "loginViewController") as! loginViewController

    // Add View Controller as Child View Controller
    self.add(asChildViewController: viewController)

    return viewController
}()

private lazy var registerViewController: registerViewController = {
    // Load Storyboard
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

    // Instantiate View Controller
    var viewController = storyboard.instantiateViewController(withIdentifier: "registerViewController") as! registerViewController

    // Add View Controller as Child View Controller
    self.add(asChildViewController: viewController)

    return viewController
}()

private func add(asChildViewController viewController: UIViewController) {
    // Add Child View Controller
    addChildViewController(viewController)

    // Add Child View as Subview
    view.addSubview(viewController.view)

    // Configure Child View
    viewController.view.frame = view.bounds
    viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    // Notify Child View Controller
    viewController.didMove(toParentViewController: self)
}

private func remove(asChildViewController viewController: UIViewController) {
    // Notify Child View Controller
    viewController.willMove(toParentViewController: nil)

    // Remove Child View From Superview
    viewController.view.removeFromSuperview()

    // Notify Child View Controller
    viewController.removeFromParentViewController()
}

private func updateView() {

    add(asChildViewController: registerViewController)

    /*
     if segmentedControl.selectedSegmentIndex == 0 {
     remove(asChildViewController: sessionsViewController)
     add(asChildViewController: summaryViewController)
     } else {
     remove(asChildViewController: summaryViewController)
     add(asChildViewController: sessionsViewController)
     }
     */
}

Credit to this guy: https://cocoacasts.com/managing-view-controllers-with-container-view-controllers/

like image 150
JDoe Avatar answered Nov 07 '22 23:11

JDoe


The easiest way to do that is to have 3 container views in Storyboard above each other, each one of them is connected to a different view controller, then in your code, make 2 of these 3 container views hidden and leave one visible based on your business needs.

This is described in detail in this blog post. Hope this helps!

like image 38
Mo Abdul-Hameed Avatar answered Nov 07 '22 22:11

Mo Abdul-Hameed