IBOutlet collectionView返回nil

时间:2019-10-14 22:35:00

标签: ios swift uitableview uicollectionview

我试图从UITableView类重新加载UICollectionView中的特定单元格,但是由于某些原因,我设置的IBOutlet始终返回nil ...

class View: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
  @IBOutlet weak var collectionView: UICollectionView!

  override func viewDidLoad() {
    super.viewDidLoad()
    if let collection = collectionView { print("Working") } else { print("not working") }
  }

  func changeImage() {
    if let collection = collectionView { print("Working") } else { print("Not working") }
  }
}

第二堂课

class TableView: UITableView, UITableViewDelegate, UITableViewDataSource {
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        View().changeImage()
  }
}

显然,这是相当简单的代码。加载Product ViewController的视图时,它会打印working,但是当我从TableView类中调用changeImage函数时,它会显示not working。我仍然只是在学习快速,所以请尝试对我放松一点;)

编辑: 我设置了一个全局变量var testCollectionView:UICollectionView?,并在ViewDidLoad中设置了testCollectionView = collectionView。这样就解决了问题

0 个答案:

没有答案