通过一个“根”ViewController控制多个子ViewControllers

时间:2017-04-17 21:23:16

标签: ios swift swift3

我正在创建一个具有根视图控制器的应用。此根视图控制器是许多其他子视图控制器的父级,它们是应用程序中的主要状态。

由于我在根目录viewDidLoad的开头将所有子项添加到根目录,所以所有子项的viewDid和viewWill方法在添加到根时都会触发,而不是在我选择显示子视图时触发控制器。

有人知道在需要之前避开孩子的viewDid或viewWill方法的好方法吗?如果没有,有没有办法控制这些子视图控制器?拜托,谢谢!

根视图控制器:

override func viewDidLoad() {
    super.viewDidLoad()

    guard let storyboard = storyboard else {
        print("ERROR: No storyboard found! Early return")
        return
    }

    // Introduction
    let introVC = storyboard.instantiateViewController(withIdentifier: "IntroID") as! IntroductionViewController
    self.configureChildViewController(childController: introVC, onView: self.view)

    // Login
    let loginVC = storyboard.instantiateViewController(withIdentifier: "LoginID") as! LoginViewController
    self.configureChildViewController(childController: loginVC, onView: self.view)

    // Move to the first VC
    self.goToVC(childVC: introVC)
}

转移到新的子视图控制器

func goToVC(childVC: UIViewController) {

    // Bring child to front
    self.view.bringSubview(toFront: childVC.view)

    // Create the moveTo animations to the VC
    childVC.didMove(toParentViewController: self)
}

配置子类

extension UIViewController {
func configureChildViewController(childController: UIViewController, onView: UIView) {

    if let view = childController.view {

        // Add the view controller
        addChildViewController(childController)

        // Add and resize the subview
        onView.addSubview(view)
        constrainViewEqual(holderView: onView, view: view)
    } else {
        print("ERROR: The child controller has no usable view")
    }
}

func constrainViewEqual(holderView: UIView, view: UIView) {
    view.translatesAutoresizingMaskIntoConstraints = false
    // Create the view's new constraints
    let pinTop    = self.createConstraint(ofType: .top,    inView: holderView, newView: view)
    let pinBottom = self.createConstraint(ofType: .bottom, inView: holderView, newView: view)
    let pinLeft   = self.createConstraint(ofType: .left,   inView: holderView, newView: view)
    let pinRight  = self.createConstraint(ofType: .right,  inView: holderView, newView: view)

    // Add the constraints
    holderView.addConstraints([pinTop, pinBottom, pinLeft, pinRight])
}

private func createConstraint(ofType: NSLayoutAttribute, inView: UIView, newView: UIView) -> NSLayoutConstraint {
    return NSLayoutConstraint( item:       newView,
                               attribute:  ofType,
                               relatedBy:  .equal,
                               toItem:     inView,
                               attribute:  ofType,
                               multiplier: 1.0,
                               constant:   0)
}

1 个答案:

答案 0 :(得分:0)

我能给你的最好建议是不要一次将所有子节点添加到父视图控制器。一次最多维护一个子视图控制器。

func goToVC(childVC: UIViewController) {
    if childViewControllers.count == 0 {

        self.addChildViewController(childVC)
        childVC.view.frame = containerView.bounds
        self.containerView.addSubview(childVC.view)
        childVC.didMove(toParentViewController: self)

    } else {

        let oldViewController = self.childViewControllers[0]
        childVC.view.frame = oldViewController.view.frame
        oldViewController.willMove(toParentViewController: nil)
        self.addChildViewController(childVC)
        self.transition(from: oldViewController, to: childVC, duration: 0, options: .transitionCrossDissolve, animations: nil) { completed in
            oldViewController.removeFromParentViewController()
            childVC.didMove(toParentViewController: self)
        }
    }
}