gpt4 book ai didi

swift - 将数据从自定义 UIView 传递到主 VC

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

我正在尝试将数据从我的自定义 UI View 传输到我的 MainVC。

在我的自定义 View (这是我的 MainVC 的一部分)中,我有一组照片,如果有人选择了他们想要的照片,它应该关闭自定义 View ,并使该照片出现在 MainVC 中。不幸的是,我无法使照片出现在 MainVC 中。我该如何去做呢?

在我的自定义 UIView 中,我有以下内容:

  • 在 DidSelectCell 内

    selectedPhotoImage = mediaArray[indexPath.row].image!
  • 当一个人选择他们希望上传那张照片时。

    func chooseScene(gestureRecognizer2: UIGestureRecognizer) {
    let swag = RegisterVC()
    swag.profilePhototoUpload.image = selectedPhotoImage

    }

在 mainVC(即 RegisterVC)中,我有以下内容:

 let profilePhotoSelction = UIView()
let profilePhototoUpload = UIImageView()
profilePhotoSelction.frame = CGRect(x: self.view.frame.size.width / 17, y: self.view.frame.size.height / 5.2, width: self.view.frame.size.width / 3.4, height: self.view.frame.size.width / 3.4)

profilePhototoUpload.frame = profilePhotoSelction.bounds
profilePhototoUpload.clipsToBounds = true
profilePhototoUpload.layer.cornerRadius = profilePhototoUpload.layer.frame.width / 2


profilePhotoSelction.layer.borderColor = UIColor.black.cgColor
profilePhotoSelction.layer.borderWidth = 2
profilePhotoSelction.layer.backgroundColor = UIColor.rgb(fromHex: 0xa3f323).cgColor
profilePhotoSelction.layer.cornerRadius = profilePhotoSelction.layer.frame.width / 2


let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(RegisterVC.uploadPhoto(gestureRecognizer:)))
gestureRecognizer.delegate = self
profilePhotoSelction.addGestureRecognizer(gestureRecognizer)

profilePhotoSelction.addSubview(profilePhototoUpload)
self.view.addSubview(profilePhotoSelction)

我怎样才能让 profilePhototoUpload View 包含该人选择的照片?

最佳答案

做到这一点的最佳方法可能是创建一个简单的协议(protocol),您的RegisterVC将遵循该协议(protocol),并在需要将该图像传递回时>RegisterVC您可以使用协议(protocol)方法。

作为一个非常简单的示例,您可以像这样定义您的协议(protocol):

protocol SelectImageDelegate {
func didSelectImage(image: UIImage)
}

然后,在您的自定义 UIView 中,您需要一个用于此委托(delegate)的变量,例如:

let delegate: SelectImageDelegate

您需要将此属性添加到您的 init 中,以便您可以在 init< 期间将 RegisterVC 设置为 delegate/.

然后,在 RegisterVC 中,您可以添加到类声明中以符合协议(protocol),因此它看起来像 class RegisterVC: UIViewController, SelectImageDelegate {...

最后,您需要实现协议(protocol)方法,并执行如下操作:

func didSelectImage(image: UIImage) {
self.profilePhotoToUpload.image = image
//Do whatever else you need with the image here...
}

关于swift - 将数据从自定义 UIView 传递到主 VC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43688750/

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