viewDidAppear被称为strangley

时间:2017-05-08 10:23:32

标签: ios swift uisegmentedcontrol viewwillappear viewdidappear

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

的图表

enter image description here

这就是我将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)
}

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

1 个答案:

答案 0 :(得分:1)

在这里,您可以使用containerView来实现此目的。 ContainerView是一个普通的UIView子类。您的UI将是这样的。您将拥有一个baseViewController,其中您将在baseViewController中查看segmentControlcontainerView

enter image description here

假设您有两个视图控制器,即viewController1viewController2。您可以将这些viewControllers作为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()
    }

}

}

感谢。