将数据从MainView传递到ContainerView

时间:2018-05-30 10:56:36

标签: ios swift swift3 swift4

我有这个故事板:

enter image description here 和这段代码:

var actualVisibleView : String? = nil

func showSubViewInContainerView(view: String, parms: String?){
        let controller = storyboard!.instantiateViewController(withIdentifier: view)

        systemContainerView.subviews.forEach { $0.removeFromSuperview() }
        systemContainerView.addSubview(controller.view)

        if view == "ConceptGalleryView" {

        }

        addChildViewController(controller)
        controller.view.translatesAutoresizingMaskIntoConstraints = false

        systemContainerView.addSubview(controller.view)

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

        controller.didMove(toParentViewController: self)

        self.actualVisibleView = view
        print("OPEN: \(view)")
    }

在左侧菜单中,我使用以下代码打开此容器视图中的各种视图:

showSubViewInContainerView(view: "View1", parms: "1")
showSubViewInContainerView(view: "View2", parms: "2")
showSubViewInContainerView(view: "View3", parms: "3")
showSubViewInContainerView(view: "View4", parms: nil)

例如,我调用了ConceptGalleryView.swift:

var idGallery = 0

    override func viewDidLoad() {
        super.viewDidLoad()
    }

如何从showSubViewInContainerView函数中保存idGallery参数 - parms?

1 个答案:

答案 0 :(得分:-1)

过去它:ViewController底部。

你必须创建公共变量,这样你才能从视图控制器到另一个变量:

class variables
{
    static var name: String = ""
}

打印出变量:

print("\(variables.name)")