0

This type of question has been asked and answered, but my specific situation returns an empty array. I first create an array in View Controller 1 (VC1). VC1 includes a collection view. I try to add a UIView to the array in cellForItem func.

/// A subclass of `UIViewController` with a `MessagesCollectionView`  object
/// that is used to display conversation interfaces.

open class MessagesViewController: UIViewController,
UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {


public var cellViewArray = NSMutableArray() 

open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath:    IndexPath) -> UICollectionViewCell {

let cell = messagesCollectionView.dequeueReusableCell(TextMessageCell.self, for: indexPath)
        cell.configure(with: message, at: indexPath, and: messagesCollectionView)

        let cellView = cell.contentView
        //print("cellView = \(cellView)")
        messageViewArray.insert(cellView, at: indexPath.row)
        //print("messageViewArray = \(String(describing: messageViewArray))")

return cell
}

I confirmed in debug that when the collection view runs, it does save the cellView to messageViewArray and neither are nil.

messageViewArray = (
"<UIView: 0x142913720; frame = (0 0; 359 140); autoresize = W+H; gestureRecognizers =  <NSArray: 0x142913d30>; layer = <CALayer: 0x1429138a0>>",
"<UIView: 0x14291bbc0; frame = (0 0; 359 256); autoresize = W+H; gestureRecognizers = <NSArray: 0x14291c1d0>; layer = <CALayer: 0x14291bd40>>",
"<UIView: 0x142929520; frame = (0 0; 359 256); autoresize = W+H; gestureRecognizers = <NSArray: 0x142929b30>; layer = <CALayer: 0x1429296a0>>",
"<UIView: 0x14298e610; frame = (0 0; 359 140); autoresize = W+H; gestureRecognizers = <NSArray: 0x14298ec20>; layer = <CALayer: 0x14298e790>>",
"<UIView: 0x142929520; frame = (0 0; 359 256); autoresize = W+H; gestureRecognizers = 
)

In View Controller 2 (VC2), I try to access messageViewArray from VC1 and do something with the UIView objects.

let messagesViewController  =  MessagesViewController()
print("messagesViewController.messageViewArray = \(messagesViewController.integerViewArray.description)")

This returns the below in debug showing empty array.

messagesViewController.messageViewArray = (
)

Why doesn't the array objects persist in VC2? What change can I make to reference messageViewArray in VC2. Thanks in advance for your time.

3
  • How are you going from VC1 to VC2? Perform Segue or programmatically? If you can add that code that would be helpful. Commented Jun 10, 2020 at 18:15
  • Not by seque. VC1 is imbedded in a View Controller that is embedded in VC2. It's a chat app, where VC1 controls the chat conversation. So there's no direct link from VC1 to VC2. Commented Jun 10, 2020 at 18:17
  • There has to be a way to link between VC1 and VC2. Can you post a picture of how this is all structured? How is it embedded? Commented Jun 10, 2020 at 18:27

3 Answers 3

1

@Frankenstein's answer is right.

The following code

let messagesViewController  =  MessagesViewController()

Creates a new instance of your MessagesViewController class with an empty instance of messageViewArray.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Do you have a suggestion for my comments on Frankenstein's answer? Haven't got it to work yet.
0

You're making a new instance of MessagesViewController in VC2 which will have the initial value for cellViewArray which is NSMutableArray() that get while printing it out as empty. Either You need to pass the instance of MessagesViewController to the instance of VC2 as weak var or pass the cellViewArray to VC2 which would be a better approach. And then you can print out the cellViewArray without creating a new instance of MessagesViewController as you have done here.

5 Comments

Hi Frankenstein, thank you. Can you make a suggestion on how to pass the array from VC1 to VC2? I try to save array using defaults.set(cellView, forKey: "arrayKey") in VC1 and get with UserDefaults.standard.object(forKey: "YourKey") as! NSArray in VC2, but I get error "Attempt to insert non-property list"
If I try the other approach and make MessageViewController weak var, I get the following warning "Instance will be immediately deallocated because variable 'messagesViewController' is 'weak'"
Your post doesn't give any information about the relation between VC1 and VC2. Modify the code you've given in your question and add how they both are related. Only then we can help you out.
I've added 3 lines to the top of my code reference to help describe how MessagesViewController is used. MessagesViewController is in a pod file that I unlocked to add code. My app subclasses it in VC3, which is then referenced in VC2. VC3 does not contain the cellForItem collection view func, which is where I need to create the array, so I can't create array in VC3. Does that help?
@JBeesky You've said you need to access an array from VC1 in VC2. How do you get from VC1 to VC2. What is their relationship. Once we know that we can help.
0

This works. You can access it directly. I'm not sure the exact reasoning, so someone else is welcome to explain.

In VC 2 - declare variable.

/// A subclass of `UIViewController` with a `MessagesCollectionView` object
/// that is used to display conversation interfaces.
open class MessagesViewController: UIViewController,
UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

public static var arrayNameInVCTwo : NSMutableArray? = nil

In VC 1 - reference variable from VC 2

let arrayNameInVCOne = MessagesViewController.arrayNameInVCTwo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.