简体   繁体   English

无法将数据从一个视图控制器推送到表视图控制器

[英]Not able to push data from one view controller to a table view controller

I ran into a strange problem while I was working on my project. 我在做项目时遇到一个奇怪的问题。 (Please bear with me, I'm a Swift beginner.) (请忍受,我是Swift初学者。)

This is what my storyboard looks like so far. 到目前为止,这就是我的故事板。 On the left is my regular view controller, and on the right is the TableViewController. 左边是我的常规视图控制器,右边是TableViewController。 the transition inbetween has a specific ID. 两者之间的转换具有特定的ID。

**This is the first View Controller.**

class TestsMenuViewController: UIViewController {
var tests = [Test]() 
var testTableViewController : TestsTableViewController?


override func viewDidLoad() {
    self.tests = DataModel().patientData
    //this just populates the array with some data.
    super.viewDidLoad()
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "testTableTransition" {
        testTableViewController = segue.destination as? TestsTableViewController
        testTableViewController?.tests = self.tests
        print(testTableViewController?.tests[0].testDescription)
        //somehow this is an empty array
    }
}

somehow the test array does not get passed to the next controller :( what am i doing wrong? 不知何故,测试数组不会传递给下一个控制器:(我在做什么错?

您可以将数据另存为NSUserDefaults,并在需要时调用保存的数据到视图

Hey I managed to solve my problem, what I just did was initialize the data that I pass to the TableViewController directly in the attributes. 嘿,我设法解决了我的问题,我所做的只是初始化直接在属性中传递给TableViewController的数据。 Then it worked! 然后成功了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM