简体   繁体   English

Swift - 推送和弹出 UIViewcontroller 不工作

[英]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.

相关问题 UIViewController push&pop中的问题 - Issue in UIViewController push & pop 弹出当前的UIViewController并推送另一个 - Pop current UIViewController and push another one 如何在Swift中显示带有UICollectionView的弹出式UIViewController? - How to show a pop up UIViewController with a UICollectionView in it in Swift? 如何防止在 swift 中多次推送 UIViewController? - How to prevent multiple push of UIViewController in swift? 如何在NavigationController上弹出UIViewController并同时推送UIView - How to pop a UIViewController on NavigationController and push UIView at the same time UIViewController hidesBottomBarWhenPushed在推/弹出动画期间将UIView放置在错误的位置 - UIViewController hidesBottomBarWhenPushed puts the UIView in the wrong place during push/pop animation Pop 和 Push 的导航在 SwiftUI 中不起作用? - Navigation of Pop and Push not working in SwiftUI? 如何使UIViewController弹出Swift SWRevealViewController? - How can I make UIViewController pop Swift SWRevealViewController? UINavigationController代码推送到UIViewController无法正常工作 - UINavigationController code push to UIViewController not working correctly 如何在 Swift UI 中推送和弹出视图? - How to push and pop views in Swift UI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM