【发布时间】:2020-03-07 15:09:41
【问题描述】:
我的ViewControllers 有问题。有谁知道为什么该代码不起作用? print 函数工作...
A 到 B
@IBAction func editButtonTapped(_ sender: Any) {
let imageCollectionView = self.storyboard?.instantiateViewController(withIdentifier: "ImageCollectionVC") as! ImageCollectionViewController
imageCollectionView.delegate = self
self.navigationController?.pushViewController(imageCollectionView, animated: true)
print("editButtonTapped")
}
}
extension ExampleViewController: ClassBDelegate {
func childVCDidComplete( with image: UIImage?) {
self.pickedImage = image!
}
}
B 到 A
protocol ClassBDelegate {
func childVCDidComplete(with image: UIImage?)
}
class ImageCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
@IBOutlet weak var imageCollectionView: UICollectionView!
var delegate: ClassBDelegate?
var tappedImage = UIImage()
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
tappedImage = images[indexPath.row]
delegate?.childVCDidComplete(with: tappedImage)
navigationController?.popViewController(animated: true)
}
这是我的完整项目:https://github.com/chriskonnerth/Wishlist
ViewController A 在我的例子中是 ExampleViewController 而 B 是 ImageCollectionViewController
更新
那是我的故事板:
ImageViewController 是右边,ExampleViewController 是左边。
【问题讨论】:
-
“不工作”是什么意思?应该发生什么,实际发生了什么?
-
什么也没发生。这就是问题
-
editButtonTapped应该带你到ImageCollectionViewController并点击cell应该带你回到ExampleViewController -
好的。那么
editButtonTapped是否打印“editButtonTapped”?如果不是,则该按钮未连接。如果是这样,问题是您没有导航控制器,因此无处可推。 -
是的,我也测试了
didSelectItemAt,它被调用了,这就是我没有得到的:(
标签: ios swift uiviewcontroller delegates