Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 无法在视图控制器中引用集合视图单元格出口_Swift_Uicollectionview - Fatal编程技术网

Swift 无法在视图控制器中引用集合视图单元格出口

Swift 无法在视图控制器中引用集合视图单元格出口,swift,uicollectionview,Swift,Uicollectionview,我的应用程序在标准UIView中包含一个静态集合视图。我需要让UIView的元素与UICollectionView中的元素对话。这里的问题是,当我通过建议的方法将一个类引用到另一个类时,UI元素的值返回nil。关于如何防止这种情况,有什么想法吗?我在下面举了一个例子 class ViewController: UIViewController { let cellIds = ["Purple Cell","Green Cell","Blue Cell","Red Cell"] let cellS

我的应用程序在标准UIView中包含一个静态集合视图。我需要让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]);


}

 }

您需要在CollectionView单元格子类中添加此变量

weak var parentVC:ViewController?
然后将其设置在cellForItemAt中

然后像这样使用它

parentVC?.wowNiceOutlet.text = "wow nice"
崩溃的原因是outlet wowNiceOutlet为零

当您以编程方式加载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

}

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

感谢您快速而有用的回答。我试着这样做的问题是,按下按钮时标签不会改变。由于optionals,它没有返回错误,但我假设标签仍然返回nil或类似的内容。有什么建议吗?因为我在上面的消息中忘记了,所以打电话给你,很抱歉。你能根据这个回答分享你的最新更新吗?你是否用ids CellId[indexPath.item]注册所有单元格?当你将它们全部强制转换到同一类型MyCollectionViewCell时,你可能只需要注册一次单元格标识符并将其与dequeueCell方法一起使用,不需要为那些该死的东西做阵列!你的编辑成功了。非常感谢你!!迫不及待地想把它应用到我需要它的项目中!!
let theViewControl = ViewController()
theViewControl.wowNiceOutlet.text = "wow nice"
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

}