-1

I have a parent view controller with 5 container view as you can see in image : enter image description here

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 ?

ZahraAsgharzade
  • 311
  • 3
  • 10
  • this is a little confusing... Do you want to **push** from each view controller to the next? – DonMag Jul 01 '19 at 12:34
  • Can you show me the code for dismissal and push to parent view controller? If you want to go through some documentation regarding container then please refer this [link](https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html) – Mubin Mall Jul 01 '19 at 12:35

1 Answers1

0

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 . I clean all segue and container views from storyboard and I did it like this :

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():

        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/

hope to help any one else :)

ZahraAsgharzade
  • 311
  • 3
  • 10
  • 1
    So there is nothing wrong with that. Here I want to mention that you can take IBOutlets of each View(container's IBOutlet) in the main parent controller. And you can hide show the view based on your requirement. Suppose you want to show FirstViewController then related IBOutlet of container view's hidden property will be false while all others will be hidden. – Mubin Mall Jul 01 '19 at 13:03
  • @MubinMall i didn't know that . thanks for your guide and information . now I exactly understand where was my problem . I didn't set any IBOutlet of my container views . – ZahraAsgharzade Jul 01 '19 at 13:31