I'm trying to go to from one VC (with implemented Xib file inside) to another VC using a segue.
However, I'm getting an error of
Cannot find 'performSegue' in scope
This is the class of my xib file:
class PetNameInfoCollectionViewCell: UICollectionViewCell {
@IBAction func takeAPhoto(_ sender: UIButton) {
performSegue(withIdentifier: "UIImagePickerSegue", sender: nil)
}
}
performSegue
is one of UIViewController
's methods, so it doesn't work on UICollectionViewCell
. Instead, you'll need to call performSegue
from the parent view controller that contains the collection view.
You can use delegates or closures for this, but I prefer closures. First, add one inside PetNameInfoCollectionViewCell
:
class PetNameInfoCollectionViewCell: UICollectionViewCell {
var photoTapped: (() -> Void)? /// here!
@IBAction func takeAPhoto(_ sender: UIButton) {
photoTapped?() /// call it
}
}
Then, assign the closure inside the parent view controller's cellForItemAt
.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userNameInfoCollectionViewCellId, for: indexPath) as! userNameInfoCollectionViewCell /// replace with the cell class
cell.photoTapped = { [weak self] in
self?.performSegue(withIdentifier: "UIImagePickerSegue", sender: nil)
}
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PetNameInfoCollectionViewCell, for: indexPath) as! PetNameInfoCollectionViewCell /// replace with the cell class
cell.photoTapped = { [weak self] in
self?.performSegue(withIdentifier: "UIImagePickerSegue", sender: nil)
}
return cell
}
}