Swift - 推送和弹出 UIViewcontroller 不工作
Swift - push & pop UIViewcontroller not working
我的 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
是左边的
检查下面代码行中的 navigationController 实例:
self.navigationController?.pushViewController(imageCollectionView, animated: true)
如果 self.navigationController
为 nil,那么您可能没有在 ExampleViewController.
中嵌入导航控制器
然后您需要将您的第一个控制器与导航控制器一起嵌入。
我的 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
是左边的
检查下面代码行中的 navigationController 实例:
self.navigationController?.pushViewController(imageCollectionView, animated: true)
如果 self.navigationController
为 nil,那么您可能没有在 ExampleViewController.
然后您需要将您的第一个控制器与导航控制器一起嵌入。