Swift 在两个 VC 之间委托,没有 segue

Swift delegate beetween two VC without segue

我有 3 个 classes:

我想从第一个VC中按下一个剪辑,然后打开媒体库选择一个图像。然后 selected 图像作为 previewController 传递给第三个 VC。然后如果我 select 'done' 我想把它传递给第一个 VC.

第一VC

class ChatLogControoller: UICollectionViewController, UICollectionViewDelegateFlowLayout, NSFetchedResultsControllerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, DataSentDelegate {

func recievePhoto(data: UIImage) {
    imageFromView = data
    print("-------\(imageFromView = data)")
}

override func viewDidLoad() {
    super.viewDidLoad()

    let vc = ImagePreviewController()
    self.vc.delegate = self
}

2nd class 它只是图像选择器,所以我将图像传递给 3rd VC 并且该图像成功出现在 3rd VC 的 imageView 上!

我的第三个VC

protocol DataSentDelegate {
    func recievePhoto(data: UIImage)
}
class PreviewController: UIViewController, UIScrollViewDelegate {

var delegate : DataSentDelegate? = nil

var aImageView: UIImageView!
var aImage: UIImage!

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(actionSend))
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(actionBack))

}

@objc func actionBack() {

    dismiss(animated: false, completion: nil)
}

@objc func actionSend() {

    let data = aImageView.image
    delegate?.recievePhoto(data: data!)
    dismiss(animated: true, completion: nil)
}

您需要在 SecondViewController 中再创建一个协议以将该委托从 ThirdViewController 传递给 FirstViewController

FirstViewController:

import UIKit

class ViewController: UIViewController, DataSentDelegate, dataSentDelegate  {

    @IBOutlet weak var imagefromThirdVC: UIImageView!

    var thirdVCImage: UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func buttonTapped(_ sender: Any) { 
        let vc = storyboard?.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
        vc.delegate = self
        self.navigationController?.pushViewController(vc, animated: true)
    }

    func goToThirdVC() {
        let vc = storyboard?.instantiateViewController(withIdentifier: "ViewController3") as! ViewController3
        vc.delegate = self
        self.navigationController?.pushViewController(vc, animated: true)
    }

    func recievePhoto(data: UIImage) {
        thirdVCImage = data
        imagefromThirdVC.image = thirdVCImage
    }
}

SecondViewController:

import UIKit

protocol dataSentDelegate {
    func goToThirdVC()
}

class ViewController2: UIViewController {

    @IBOutlet weak var passingImage: UIImageView!

    var delegate: dataSentDelegate? = nil

    var images: UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()

        images = UIImage(named: "screen")
    }

    @IBAction func actionButton(_ sender: Any) {
        self.delegate?.goToThirdVC()
    }

}

ThirdViewController:

import UIKit

protocol DataSentDelegate {
    func recievePhoto(data: UIImage)
}

class ViewController3: UIViewController {

    var delegate: DataSentDelegate? = nil

    @IBOutlet weak var passedImageView: UIImageView!

    var passedImage: UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()

        passedImage = UIImage(named: "screen")
        passedImageView.image = passedImage
    }

    @IBAction func action(_ sender: Any) {

        let data = passedImageView.image
        delegate?.recievePhoto(data: data!)
        //   delegate?.goToFirstVC()

        guard let viewControllers = self.navigationController?.viewControllers else {
            return
        }

        for firstViewController in viewControllers {
            if firstViewController is ViewController {
                self.navigationController?.popToViewController(firstViewController, animated: true)
                break
            }
        }

    }

}