viewDidAppear 被称为 strangley

viewDidAppear being called strangley

我有一个 viewController 包含 segmentedControl。我有一个 VCA 和 VCB,它们在 segmentedControl 中。当我点击第二段 VCB 时出现。现在我正在推另一个来自 VCB 的 ViewController。但是当从 viewController 返回时,正在调用 VCA 的 viewDidAppear。这对我来说很奇怪。因为用户在 VCB 上,所以为什么要调用 VCA 的 viewWillAppearviewDidAppear?这里有图解释更多

这就是我将 viewControllers 添加到 segmentedControl

的方式
 func switchToViewController(viewController: UIViewController, selectedIndex: Int) {

    viewController.removeFromParentViewController()
    viewController.view.removeFromSuperview()

    addChildViewController(viewController)
    viewController.view.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(viewController.view)

    // Setting constraints of the container view
    NSLayoutConstraint.activate([
    viewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
        viewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
        viewController.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
        viewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
        ])

    viewController.didMove(toParentViewController: self)
}

我只是无法理解这种行为。所以请指导我。

您永远不会从层次结构中删除 current 视图控制器及其视图...

您需要跟踪当前显示的 VC/view - 也许使用 currentVC 变量,您的函数应如下所示:

func switchToViewController(viewController: UIViewController, selectedIndex: Int) {

    // remove current ViewController from VC hierarchy
    currentVC.removeFromParentViewController()

    // remove current VC.View from View hierarchy
    currentVC.view.removeFromSuperview()

    // the "incoming" ViewController becomes the "current" ViewController
    currentVC = viewController

    addChildViewController(viewController)
    viewController.view.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(viewController.view)

    // Setting constraints of the container view
    NSLayoutConstraint.activate([
    viewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
        viewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
        viewController.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
        viewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
        ])

    viewController.didMove(toParentViewController: self)
}

给你,你可以通过使用 containerView 来实现。 ContainerView 是一个普通的 UIView 子类。你的 UI 会是这样的。您将拥有一个 baseViewController,其中您将在 baseViewController 中拥有 segmentControlcontainerView 视图。

假设您有两个视图控制器,即 viewController1viewController2。您可以将这些 viewController 作为 childViewController 添加到此 containerView 中,如下所示。

import UIKit

class BaseViewController: UIViewController {

    @IBOutlet weak var typeSegment: UISegmentedControl!
    @IBOutlet weak var containerView: UIView!

    var viewController1: UIViewController?
    var viewController2: UIViewController?


    // MARK: - Action method.

    @IBAction func segmentIndexChanged(_ sender: Any) {

        let selectedIndex = typeSegment.selectedSegmentIndex

        switch selectedIndex {
        case 0:
            addVC1()
        case 1:
            addVC2()
        default:
            break
        }

    }

func rectForChildVC() -> CGRect {

    let rect = CGRect(x: containerView.frame.origin.x , y: containerView.frame.origin.y, width: containerView.frame.size.width, height: containerView.frame.size.height)
    return rect

}

func addVC1() {

    removeVC2()
    let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
    viewController1 = storyboard.instantiateViewController(withIdentifier: "Viewcontroller1Identifier") // Create you first view controller instance here.
    viewController1?.view.frame = rectForChildVC()
    addChildViewController(viewController1!)
    view.addSubview((viewController1?.view)!)
    viewController1?.didMove(toParentViewController: self)
    view.layoutIfNeeded()

}

func addVC2() {

    removeVC1()
    let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
    viewController2 = storyboard.instantiateViewController(withIdentifier: "Viewcontroller2Identifier") // Create you second view controller instance here.
    viewController2?.view.frame = rectForChildVC()
    addChildViewController(viewController2!)
    view.addSubview((viewController2?.view)!)
    viewController2?.didMove(toParentViewController: self)
    view.layoutIfNeeded()

}

func removeVC1() { // Remove first view controller.

    if let viewController = viewController1 {
        viewController.didMove(toParentViewController: nil)
        viewController.view.removeFromSuperview()
        viewController.removeFromParentViewController()
    }

}

func removeVC2() { // Remove second view controller

    if let viewController = viewController2 {
        viewController.didMove(toParentViewController: nil)
        viewController.view.removeFromSuperview()
        viewController.removeFromParentViewController()
    }

}

}

谢谢。