无法在View Controller中引用集合View单元出口

时间:2018-10-19 11:28:50

标签: swift uicollectionview

我的应用程序在标准UIView的内部包含一个静态的Collection视图。我需要让UIView的元素与UICollectionView中的元素对话。这里的问题是,当我通过建议的方法将一个类引用到另一个时,UI元素的值返回nil。关于如何防止这种情况的任何想法?我在下面提供了一个示例。

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

 }

谢谢!让我知道您是否有任何疑问!

根据Sh_Khan更新的代码:

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

var thisString = "nice string"
   //Theoretically thisString would be changed by a Stepper and upon doing that 
   //would be changing visual properties of the, in this case, label.
@IBOutlet weak var wowNiceOutlet: UILabel!

}
 class MyCollectionViewCell: UICollectionViewCell {
weak var parentVC:ViewController?
@IBOutlet var greenLabel: UILabel!
@IBOutlet var myButton: UIButton!
@IBAction func myButtonPressed(_ sender: UIButton) {
    myButton.setTitle("eat UICollectionView", for: .normal)

    parentVC?.wowNiceOutlet.text = "wow nice"
}

 }
  extension ViewController: UICollectionViewDataSource {
func collectionView( _ collectionView: UICollectionView, numberOfItemsInSection 
  section: Int) -> Int {
    return cellIds.count

}
func collectionView( _ collectionView: UICollectionView, cellForItemAt 
  indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: 
  cellIds[indexPath.item], for: indexPath) as! MyCollectionViewCell // Get cell
    cell.parentVC = self
    cell.greenLabel.text = thisString
    return collectionView.dequeueReusableCell( withReuseIdentifier: 
 cellIds[indexPath.item], for: indexPath)

}

 }
 extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout 
 collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: 
  IndexPath) -> CGSize {
    return cellSizes[indexPath.item]

}

 }
  extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt 
  indexPath: IndexPath) {
    print(cellIds[indexPath.row]);


}

 }

2 个答案:

答案 0 :(得分:1)

您需要将此变量添加到CollectionView单元子类中

weak var parentVC:ViewController?

然后将其设置在cellForItemAt

cell.parentVC = self

然后像这样使用它

parentVC?.wowNiceOutlet.text = "wow nice"

崩溃的原因是出口wowNiceOutlet为零

let theViewControl = ViewController()
theViewControl.wowNiceOutlet.text = "wow nice"

当您以编程方式加载vc而不是从storyborad.instantiate加上另一个实例


您返回的另一个单元格实例不同于您将委托设置为的那个实例

func collectionView( _ collectionView: UICollectionView, cellForItemAt 
  indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: 
  cellIds[indexPath.item], for: indexPath) as! MyCollectionViewCell // Get cell
    cell.parentVC = self

    cell.someLabel.text = "anyvalue" // <<< edit here 

    return cell

}

答案 1 :(得分:0)

您正在创建一个新的let theViewControl = ViewController(),它不同于以前的ViewController。您必须将其从原始的ViewController传递到单元格。