Group Group Group Group Group Group Group Group Group

Can't Reference Collection View Cell Outlets in ViewController

#1

My app contains a static Collection View inside of a standard UIView. I need to let elements of the UIView talk to elements in the UICollectionView. The issue here is that when I go through suggested methods to reference one of the classes to the other, the value of the UI Element returns nil. Any ideas as to how this can be prevented? I’ve included an example below.

class ViewController: UIViewController {
let cellIds = ["Purple Cell","Green Cell","Blue Cell","Red Cell"]
let cellSizes = Array(repeatElement(CGSize(width: 170, height: 80), count: 4))
     override func viewDidLoad() {
         super.viewDidLoad()
         // Do any additional setup after loading the view, typically from a nib.
     }

@IBOutlet weak var wowNiceOutlet: UILabel!

 }
 class MyCollectionViewCell: UICollectionViewCell {
     @IBOutlet var myButton: UIButton!
     @IBAction func myButtonPressed(_ sender: UIButton) {
         myButton.setTitle("eat UICollectionView", for: .normal)
         let theViewControl = ViewController()
         **theViewControl.wowNiceOutlet.text = "wow nice"** //This line returns nil, causing an error.
     }

 }

Thank you! Let me know if you have any questions!

opened #3
#4

Hi @callmetyler,
Do you have a custom cell? If not then you get default access to the elements of a cell. A better way would be to subclass the cell and use it from the class.

cheers,