我正在尝试将数据从我的自定义 UI 视图传输到我的 MainVC。
在我的自定义视图(它是我的 MainVC 的一部分)中,我有一组照片,如果有人选择了他们想要照片,它应该关闭自定义视图,并使该照片出现在 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 视图包含该人选择的照片?
做到这一点的最好方法可能是创建一个protocol
您RegisterVC
将遵守的简单方法,并且当需要将该图像传递回时,RegisterVC
您可以使用协议方法。
作为一个非常简单的例子,你可以这样定义protocol
:
protocol SelectImageDelegate {
func didSelectImage(image: UIImage)
}
然后,在您的自定义 UIView 中,您需要为该委托设置一个变量,例如:
let delegate: SelectImageDelegate
您需要将此属性添加到您的,init
以便您可以设置RegisterVC
为delegate
during init
。
然后,RegisterVC
您可以添加到类声明以符合协议,所以它看起来像class RegisterVC: UIViewController, SelectImageDelegate {...
最后,您需要实现协议方法,并执行如下操作:
func didSelectImage(image: UIImage) {
self.profilePhotoToUpload.image = image
//Do whatever else you need with the image here...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句