[英]Swift - push & pop UIViewcontroller not working
I am having problems with my ViewControllers
.我的ViewControllers
有问题。 Does anyone know why that code is not working?有谁知道为什么该代码不起作用? The print
function work... print
function 工作...
A to 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 to A 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)
}
That is my full project: https://github.com/chriskonnerth/Wishlist这是我的完整项目: https://github.com/chriskonnerth/Wishlist
ViewController A in my case is ExampleViewController
and B is ImageCollectionViewController
ViewController A 在我的例子中是ExampleViewController
而 B 是ImageCollectionViewController
UPDATE更新
that is my Storyboard:那是我的 Storyboard:
ImageViewController
is the right one and ExampleViewController
is the one left to it. ImageViewController
是右边, ExampleViewController
是左边。
Check navigationController instance in below line of code:在下面的代码行中检查 navigationController 实例:
self.navigationController?.pushViewController(imageCollectionView, animated: true)
If self.navigationController
is nil then you may have not embedded navigation controller with your ExampleViewController .如果self.navigationController
为 nil,那么您可能没有使用您的 ExampleViewController 嵌入导航controller 。
Then you need to embed your first controller with navigation controller.然后您需要使用导航 controller 嵌入您的第一个 controller。 To add navigation controller - 添加导航 controller -
Have you add StoryBoard ID
in storyboard?你有没有在 storyboard 中添加StoryBoard ID
? If not,add the identifier( ImageCollectionVC
) to the storyboard.如果没有,请将标识符 ( ImageCollectionVC
) 添加到 storyboard。
Then try this:然后试试这个:
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ImageCollectionVC") as! ImageCollectionViewController
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.