我正在尝试将数据从我的自定义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视图显示该人选择的照片?
答案 0 :(得分:0)
执行此操作的最佳方法可能是创建一个protocol
符合的简单RegisterVC
,以及何时将该图像传回RegisterVC
即可使用协议方法。
作为一个非常简单的示例,您可以像这样定义protocol
:
protocol SelectImageDelegate {
func didSelectImage(image: UIImage)
}
然后,在你的自定义UIView中,你需要一个这个委托的变量,如:
let delegate: SelectImageDelegate
您需要将此属性添加到init
,以便在RegisterVC
期间将delegate
设置为init
。
然后,在RegisterVC
中,您可以添加到类声明以符合协议,因此它看起来像class RegisterVC: UIViewController, SelectImageDelegate {...
最后,您需要实现协议方法,并执行以下操作:
func didSelectImage(image: UIImage) {
self.profilePhotoToUpload.image = image
//Do whatever else you need with the image here...
}
答案 1 :(得分:0)
您可以尝试这种方式:
1)。在您的自定义UIView定义MainVC类型的变量
var mainVC: MainVC?
2)。当您打开自定义UIView时,从MainVC将其新创建的mainVC
设置为self
。
3)。在此视图中,控制器添加方法以显示图像,即
func showSelectedImage(image: UIImage) {
....
}
4)。现在从自定义UIView控制器中选择图像并且您要关闭自定义UIView从MainVC调用该方法,如下所示:
self.showSelectedImage(image:theSelectedImage)
这是解决问题的方法之一,当然您也可以使用协议。