将图像从弹出窗口发送到父视图

时间:2017-05-16 16:50:17

标签: xcode swift3 xcode8

我有一个视图,打开另一个视图Controller作为popover

popover使用图像选择器并在imageView中显示所选图像。 父母也有一个带有名为profiler的IBoutlet的imageView,在解除popover之前,我希望父视图能够获得所选择的图像。

我将如何访问父级并将图像从弹出窗口发送到父视图

下面是popover视图控制器的代码:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        picker.delegate = self



    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }





    //MARK: - Delegates
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
        myImageView.contentMode = .scaleAspectFit //3
        myImageView.image = chosenImage //4


        myImageView.layer.borderWidth = 1
        myImageView.layer.masksToBounds = false
        myImageView.layer.borderColor = UIColor.black.cgColor
        myImageView.layer.cornerRadius = myImageView.frame.height/4
        myImageView.clipsToBounds = true



        dismiss(animated:true, completion: nil) //5
    }

感谢您提供的任何帮助

2 个答案:

答案 0 :(得分:2)

使用回调闭包,它比委托/协议

更省力
  • 在弹出窗口控制器中添加属性

    weak var callback : ((UIImage) -> ())?
    
  • didFinishPickingMediaWithInfo

    之前在dismiss中调用它
    callback?(chosenImage)
    
  • 在创建控制器后的父视图控制器中添加

    popoverViewController.callback = { image in
        // do something with the image
    }
    

popoverViewController是弹出视图控制器实例

答案 1 :(得分:0)

你应该创建协议

protocol PopOverImagePickerDelegate: NSObjectProtocol {
    func didSelect(image: UIImage)
}

PopoverViewController创建

weak var delegate: PopOverImagePickerDelegate?

PopoverViewController设置

导航到ParentViewController之前
popOverViewController.delegate = self

然后创建

extension ParentViewController: PopOverImagePickerDelegate {
    func didSelect(image: UIImage) {
        //do stuff
    }
}

当在PopoverViewController内选择图像时,只需调用

self.delegate?.didSelect(image: image)