Xcode - How can i programmatically embed/change vi

2020-05-26 10:23发布

问题:

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.

回答1:

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/



回答2:

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!