使用Swift重置ContainerView

时间:2019-04-28 03:40:25

标签: swift

我正在测验。视图在容器中提出了一个问题。

现在容器可能有一个已有的问题(需要替换该问题),或者可能是第一个在容器中放置问题的实例。

@IBOutlet weak var containerView: UIView!

func createContainerView() {
    if let questionSet = quizManagerClass.getNextQFromSet() {
        question = questionSet.question
        answers = questionSet.answers

        if containerView != nil && containerView.isDescendant(of: view) {
            containerView.removeFromSuperview()
        }

        // add container
        let localContainerView = UIView()
        localContainerView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(localContainerView)
        NSLayoutConstraint.activate([
            localContainerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
            localContainerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
            localContainerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0),
            localContainerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0),
            ])

        // add child view controller view to container
        if let controller = storyboard!.instantiateViewController(withIdentifier: "question") as? QuestionViewController {
            controller.question = question
            childQuiz = controller
            addChild(controller)
            controller.view.translatesAutoresizingMaskIntoConstraints = false
            localContainerView.addSubview(controller.view)

            NSLayoutConstraint.activate([
                controller.view.leadingAnchor.constraint(equalTo: localContainerView.leadingAnchor),
                controller.view.trailingAnchor.constraint(equalTo: localContainerView.trailingAnchor),
                controller.view.topAnchor.constraint(equalTo: localContainerView.topAnchor),
                controller.view.bottomAnchor.constraint(equalTo: localContainerView.bottomAnchor)
                ])

            controller.didMove(toParent: self)
        }

        containerView = localContainerView

        loadingViewRemove()
    }
}

现在拥有本地容器视图并像这样交换出来似乎是错误的。

如何使用最佳实践将容器视图替换为包含测验问题的新实例?

0 个答案:

没有答案