0

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. I am sending values for prioritycolor and note from firstVC.

I will be setting up CoreData later on, for now I just want this to work in simplest of manners. I am appending data from firstVC to this array like so:

@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.

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. 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.

I don't know what I am doing wrong here. Please help me!

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
Osama Naeem
  • 1,830
  • 5
  • 16
  • 34

1 Answers1

1

Thats because you end up getting a new instance of AddedNotesCollectionViewController every time you press the button.

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 :)

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. There are many answers in SO which explains how to access the pushed/modally presented 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.

If in case your AddedNotesCollectionViewController is never presented then rather consider creating singleton instance of notesArray which you will share between multiple VCs.

Hope it helps

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • @osama-naeem : One word : Bingo!!! but that being said there are multiple ways to pass data between VCs you can choose the one that suites you better :) I would go for segue hence I said Bingo – Sandeep Bhandari Nov 10 '17 at 09:21
  • Thank you so much Sandeep! Will get on it and get back to you :) Thanks! – Osama Naeem Nov 10 '17 at 09:27
  • Hey @Sandeep-Bhandari, Just wanted to ask if you could help me. I am resizing my collectionviewcells dynamically but when I delete a particular cell, the remaining cells' content get off. Here is a detailed post: https://stackoverflow.com/questions/47235608/cell-layout-refresh-after-deleting-one-cell-in-uicollectionview any idea what's going wrong here? – Osama Naeem Nov 11 '17 at 08:54