Xcode - 如何以编程方式在容器视图中嵌入/更改视图控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45118016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:08:05  来源:igfitidea点击:

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

xcodeviewswift3controllercontainers

提问by JDoe

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

我正在尝试以编程方式设置/或更改Controller View内的嵌入式View Controller

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.

我有 3 个不同的视图控制器,我想在容器视图中显示它们,这一切都取决于用户是否登录。

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.

我尝试过的很多代码也没有出现在 Swift 3 中,所以作为一个新的应用程序开发人员,这压力很大,因为我试图将其转换为 Swift 3。

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

任何人都可以提供更改视图容器内的嵌入式视图控制器的解决方案吗?谢谢。

回答by JDoe

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 Controllerand 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/

归功于这个人:https: //cocoacasts.com/managing-view-controllers-with-container-view-controllers/

回答by Moe

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.

最简单的方法是在 Storyboard 中将 3 个容器视图放在一起,每个视图都连接到不同的视图控制器,然后在您的代码中,隐藏这 3 个容器视图中的 2 个,并根据您的业务需要。

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

这在这篇博文中有详细描述。希望这可以帮助!