【发布时间】:2018-04-12 17:50:40
【问题描述】:
我有一个很奇怪的问题。我有名为NewAdCreationViewController 的父VC,它以模态方式呈现名为CollectionPicturesViewController 的子VC。
在子 VC 中,我有到父 VC 的链接。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == picturesCollectionVCSegueName {
let collectionController = (segue.destination as! UINavigationController).viewControllers.first as! CollectionPicturesViewController
let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as? PicturesCollectionTableViewCell
guard cell != nil else{ return }
collectionController.picturesArray = cell!.itemPictures
collectionController.parentVC = self // this is link!
}
}
在子 VC 中,我介绍了另一个用于挑选照片的 VC - DKImagePickerController(pod)。选择我的所有照片后,子 VC 的 collectionView 中出现,当我点击“保存”时,我想将所有数据传递给父 VC 并解雇子,
@objc func savePictures(){
self.parentVC.pictures = self.picturesArray
self.parentVC.tableView.reloadData()
self.dismiss(animated: true, completion: nil)
}
但在我关闭它之后,我的父母会完全重新加载并从viewDidLoad 开始。它提供了全新的 VC(我已经在控制台中检查过,它在内存中有另一个地址)。我真的不知道这是为什么?
【问题讨论】:
-
请显示更多信息!
parentVC是如何定义的?这是个什么样的segue?你能展示一下这些视图控制器是如何关联的吗?如果你能更全面地解释它,我相信我们可以很容易地解决这个问题。谢谢。 -
@matt 我添加了一些截图。
parentVC只是NewAdCreationViewController的财产 -
我建议的两件事。一种是在将 parentVC 分配给子变量时使用
weak self。另一个是确保您在ViewDidAppear中没有一些时髦的代码或一些更简单的解释。 -
另外,刚刚看到您的屏幕截图,您可能应该将子 VC 推送到您已有的导航控制器上,而不是实例化一个新的。
-
@NSGangster 是的,我可以做到,但我需要以模态方式呈现它,因此我需要带有 BarButtons 的导航栏,这就是我创建新导航控制器的原因