简体   繁体   English

如何管理应该在父视图控制器中显示哪个控制器?

[英]how to manage which controller should show in parent view controller?

I have a parent view controller with 5 container view as you can see in image :我有一个带有 5 个容器视图的父视图控制器,如您在图像中看到的: 在此处输入图片说明

but when I run my app ,all child view controllers are shown blow by blow and they dismiss and goes back to my starter view controller (which is initial view controller and I push my parent navigation controller form it).但是当我运行我的应用程序时,所有子视图控制器都一一显示,它们会关闭并返回到我的起始视图控制器(这是初始视图控制器,我将父导航控制器从它推入)。 I want to know how to prevent it and how to show my first view controller when parent view controller showed ?我想知道如何防止它以及如何在父视图控制器显示时显示我的第一个视图控制器?

I don't know what is wrong with storyboard but my problem was : because I adde 5 container view to my main view controller and connected all of them to their view controllers by segue it presents all of them and then close main view controller .我不知道 storyboard 有什么问题,但我的问题是:因为我将 5 个容器视图添加到我的主视图控制器并通过 segue 将它们全部连接到它们的视图控制器,它呈现所有这些,然后关闭主视图控制器。 I clean all segue and container views from storyboard and I did it like this :我从故事板中清除了所有 segue 和容器视图,我是这样做的:

private lazy var firstViewController: AvailableView = {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        var viewController = storyboard.instantiateViewController(withIdentifier: "AvailableViewID") as! AvailableView
        self.add(asChildViewController: viewController)

        return viewController
    }()

    private lazy var secondViewController: NotificationView = {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        var viewController = storyboard.instantiateViewController(withIdentifier: "NotificationViewID") as! NotificationView
        self.add(asChildViewController: viewController)
        return viewController
    }()

    private func add(asChildViewController viewController: UIViewController) {
        addChild(viewController)
        view.addSubview(viewController.view)
        viewController.view.frame = view.bounds
        viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        viewController.didMove(toParent: self)
    }

    private func remove(asChildViewController viewController: UIViewController) {
        viewController.willMove(toParent: nil)
        viewController.view.removeFromSuperview()
        viewController.removeFromParent()
    }

and the way you can use it : in viewDidLoad() :以及您可以使用它的方式:在viewDidLoad()

        add(asChildViewController: firstViewController)

and when you wanted to present second view controller you should remove first View Controller and then add your second view controller like so:当你想展示第二个视图控制器时,你应该删除第一个视图控制器,然后像这样添加第二个视图控制器:

remove(asChildViewController: firsttViewController)
add(asChildViewController: secondViewController)

you can see this link for more explanation : https://cocoacasts.com/managing-view-controllers-with-container-view-controllers/您可以查看此链接以获取更多解释: https : //cocoacasts.com/managing-view-controllers-with-container-view-controllers/

hope to help any one else :)希望对其他人有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM