简体   繁体   English

secondVC中的结构数组未从firstVC追加

[英]Struct Array in secondVC not being appended from the firstVC

I have created a struct and made an array of that type. 我创建了一个结构并制作了该类型的数组。 The struct consists of two variable: 该结构包含两个变量:

struct notesarray
{
    var prioritycolor : UIColor
    var note : String
}

In my secondVC which houses a collectionViewController, I have made an array of type notesarray. 在我的第二个VC中,它包含一个collectionViewController,我制作了一个类型为notesarray的数组。 I am sending values for prioritycolor and note from firstVC. 我正在从firstVC发送prioritycolor和note的值。

I will be setting up CoreData later on, for now I just want this to work in simplest of manners. 稍后我将设置CoreData,现在我只希望它以最简单的方式工作。 I am appending data from firstVC to this array like so: 我将数据从firstVC附加到此数组,如下所示:

@objc func handleCheckButton()
{
    print("Added")
    let secondVC = AddedNotesCollectionViewController()

    secondVC.allnotes.append(notesarray(prioritycolor: taskTextView.backgroundColor!, note: taskTextView.text))
    print(secondVC.allnotes.count)

    taskTextView.text = nil

}

allnotes is the name of the array found in secondVC. allnotes是secondVC中找到的数组的名称。

For testing purposes I am printing secondVC.allnotes.count but I am just getting '1' in console no matter how many time I add elements to the array. 出于测试目的,我正在打印secondVC.allnotes.count但是无论我向数组中添加元素多少次,我都只会在控制台中得到“ 1”。 I have also tested this by placing print(allnotes.count) under viewDidAppear func in secondVC so that whenever I go to secondVC it gives me count of the elements in the array but it also shows '0' every time. 我还通过将print(allnotes.count)放置在secondVC的viewDidAppear函数下进行了测试,以便每当我访问secondVC时,它都会为我提供数组中元素的数量,但每次也显示为“ 0”。

I don't know what I am doing wrong here. 我不知道我在做什么错。 Please help me! 请帮我!

Thats because you end up getting a new instance of AddedNotesCollectionViewController every time you press the button. 那是因为每次按下按钮时,您AddedNotesCollectionViewController都会获得一个AddedNotesCollectionViewController的新实例。

let secondVC = AddedNotesCollectionViewController()

And new instance is initiated with an empty array and you add one element to it by calling 新实例以一个空数组启动,您可以通过调用添加一个元素

secondVC.allnotes.append(notesarray(prioritycolor: taskTextView.backgroundColor!, note: taskTextView.text))

Hence count is always one. 因此,计数始终为一。 iOS is correct there my friend :) iOS是正确的,我的朋友:)

What you need: 您需要什么:

If second VC is already loaded either by pushing a it on to navigation stack of FirstVC or if its presented then get the reference to the presented/pushed VC rather than creating a new one every time. 如果已经将第二个VC推入FirstVC的导航堆栈中,或者如果已显示第二个VC,则获取该VC的引用,而不是每次都创建一个新的VC。 There are many answers in SO which explains how to access the pushed/modally presented VC :) 在SO中有很多答案,它们解释了如何访问推送/模态呈现的VC :)

If you are about to present/push the SecondVC, as you mentioned in the comments you can always make use of prepareForSegue to pass the data. 如您在注释中提到的那样,如果您要展示/推送SecondVC,则始终可以使用prepareForSegue传递数据。

If in case your AddedNotesCollectionViewController is never presented then rather consider creating singleton instance of notesArray which you will share between multiple VCs. 如果万一从没出现您的AddedNotesCollectionViewController那么可以考虑创建notesArray singleton instance ,该singleton instance将在多个VC之间共享。

Hope it helps 希望能帮助到你

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

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