gpt4 book ai didi

ios - 如何从@objc 函数在 UIViewController 之间传递数据数组

转载 作者:行者123 更新时间:2023-11-28 13:43:49 27 4
gpt4 key购买 nike

我有一个子 UICollectionViewController,其中有一组图像。当我删除任何照片时,我想将该组更新后的图像发送回父级 UIViewController。同样在子 Controller 中,我有一个编程 View ,当我单击任何图像以展开它时调用它。当图像展开时,用户可以单击删除按钮从该阵列中删除照片。删除后我的数组已正确更新,但由于某些原因我无法将其发送回父级。我尝试使用委托(delegate)和协议(protocol)将其发回。这是我的子 Controller 代码:

protocol ListImagesDelegate {

func receiveImagesUpdated(data: [String]?)
}

class ListImagesVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

// Properties
var receivedImagesPath: [String]? = []
var fullscreenImageView = UIImageView()
var indexOfSelectedImage = 0
var imagesAfterDelete: [String]? = []
var delegate: ListImagesDefectDelegate?


override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

print("imagesAfterDelete: \(imagesAfterDelete ?? [])") // I'm getting the right number of images in this array.

delegate?.receiveImagesUpdated(data: imagesAfterDelete)
}


...
...

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

print("Click on photo: \(indexPath.item + 1)")

if let imagePath = receivedImagesPath?[indexPath.item] {

guard let selectedImage = loadImageFromDiskWith(fileName: imagePath) else {return}

setupFullscreenImageView(image: selectedImage)

indexOfSelectedImage = indexPath.item
}
}

private func setupFullscreenImageView(image: UIImage){

fullscreenImageView = UIImageView(image: image)
fullscreenImageView.frame = UIScreen.main.bounds
fullscreenImageView.backgroundColor = .black
fullscreenImageView.contentMode = .scaleAspectFit
fullscreenImageView.isUserInteractionEnabled = true

self.view.addSubview(fullscreenImageView)
self.navigationController?.isNavigationBarHidden = true
self.tabBarController?.tabBar.isHidden = true

let deleteButton = UIButton(frame: CGRect(x: fullscreenImageView.bounds.maxX - 50, y: fullscreenImageView.bounds.maxY - 75, width: 30, height: 40))
deleteButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
deleteButton.backgroundColor = .black
deleteButton.setImage(UIImage(named: "trashIcon"), for: .normal)
deleteButton.addTarget(self, action: #selector(deleteButtonTapped), for: .touchUpInside)

fullscreenImageView.addSubview(deleteButton)
}

@objc func deleteButtonTapped(button: UIButton!) {
print("Delete button tapped")

receivedImagesPath?.remove(at: indexOfSelectedImage)

imagesAfterDelete = receivedImagesPath

collectionView.reloadData()

self.navigationController?.isNavigationBarHidden = false
self.tabBarController?.tabBar.isHidden = false
fullscreenImageView.removeFromSuperview()
}


这是父 Controller :


var updatedImages: [String]? = []

...
...

extension NewAlbumVC: ListImagesDelegate {

func receiveImagesUpdated(data: [String]?) {

print("New array: \(data ?? [])") // This print is never called.

updatedImages = data

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if segue.identifier == "goToImages" {

let listImagesVC = segue.destination as! ListImagesVC

listImagesVC.delegate = self
}
}
}

我想指定我的子 Controller 设置了 Storyboard ID ("ListImagesID") 以及从父到子的 segue 标识符 ("goToImages")。会导致这有什么冲突吗?

感谢您阅读本文。

最佳答案

看来委托(delegate)在这里是nil

delegate?.receiveImagesUpdated(data: imagesAfterDelete)

为此

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

触发你必须有

self.performSegue(withIdentifier:"goToImages",sender:nil)

编辑:这个

let listImagesDefectVC = storyboard?.instantiateViewController(withIdentifier: "ListImagesDefectID") as!     ListImagesDefectVC
listImagesDefectVC.receivedImagesPath = imagesPath
navigationController?.pushViewController(listImagesDefectVC, animated: true)

不会触发 prepareForSegue ,所以添加

listImagesDefectV.delegate = self

最后

解决方案 1:

@objc func tapOnImageView() { 
let listImagesDefectVC = storyboard?.instantiateViewController(withIdentifier: "ListImagesDefectID") as! ListImagesDefectVC
listImagesDefectVC.receivedImagesPath = imagesPath
listImagesDefectVC.delegate = self
navigationController?.pushViewController(listImagesDefectVC, animated: true)
}

解决方案 2:

 @objc func tapOnImageView() {  
self.performSegue(withIdentifier:"goToImages",sender:nil)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if segue.identifier == "goToImages" {

let listImagesVC = segue.destination as! ListImagesVC
listImagesVC.receivedImagesPath = imagesPath
listImagesVC.delegate = self
}
}

关于ios - 如何从@objc 函数在 UIViewController 之间传递数据数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55697122/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com