Xcode - 我如何以编程方式在容器视图中嵌入/更改视图控制器? [英] Xcode - How can i programmatically embed/change view controller within a container view?

查看:36
本文介绍了Xcode - 我如何以编程方式在容器视图中嵌入/更改视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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.

推荐答案

我可能已经找到了解决方案.我在这里回答,以防它可以帮助其他任何人.

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

我所做的是添加一个新的 View Controller,然后将其嵌入到 View Container - 这将用作主视图" - 然后我使用这个空白视图控制器来决定应该在空白的自我内更改哪个其他视图控制器.

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)
     }
     */
}

感谢这个人:https://cocoacasts.com/管理视图控制器与容器视图控制器/

这篇关于Xcode - 我如何以编程方式在容器视图中嵌入/更改视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆