gpt4 book ai didi

swift - 从 collectionViewCell 类中呈现 UIImagePickerController

转载 作者:行者123 更新时间:2023-11-30 12:14:27 25 4
gpt4 key购买 nike

我在从 collectionViewCell 呈现 UIImagePicker 时遇到了一些问题。我知道 collectionViewCells 默认情况下不充当选择器委托(delegate),因此我尝试从 UIViewController 类调用一个方法来操作持有点击的 imageView 的单元格。我的控制台中没有显示任何错误,但是当我运行应用程序并尝试呈现 UIImagePickerController 时,没有任何反应,并且我收到“警告:尝试呈现不在窗口层次结构中的 View !”。我想当用户点击 profileImageView 时呈现 UIImagePickerController。预先感谢您的帮助!

//LoginCell 类持有 imageView 以从 LoginController 类调用 UIImagePickerController 方法

       lazy var profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(handleSelectProfileImage)))
imageView.isUserInteractionEnabled = true
return imageView
}()

var loginController: LoginController?

func handleSelectProfileImage() {
let loginController = LoginController()
loginController.showImagePicker(sendingVC: loginController)
}

//LoginController 类作为 UIImagePickerController 委托(delegate)

    func showImagePicker(sendingVC: LoginController) {
let picker = UIImagePickerController()

picker.delegate = self
picker.allowsEditing = true

present(picker, animated: true, completion: nil)

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let loginCell = collectionView.dequeueReusableCell(withReuseIdentifier: loginCellId, for: indexPath) as! LoginCell
loginCell.delegate = self
return loginCell
}

最佳答案

在你的手机中试试这个:

func handleSelectProfileImage() {

guard let loginController = delegate as? LoginViewController else {
return
}

loginController.showImagePicker()
}

在您的 LoginController 中

func showImagePicker() {

let picker = UIImagePickerController()

picker.delegate = self
picker.allowsEditing = true

present(picker, animated: true, completion: nil)
}

关于swift - 从 collectionViewCell 类中呈现 UIImagePickerController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45602659/

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