【问题标题】:Swift delegate beetween two VC without segue没有segue的两个VC之间的Swift委托
【发布时间】:2018-12-19 06:03:25
【问题描述】:

我有 3 个课程:

  • 聊天记录控制器
  • GetImageFromLibraty(NSObject 类)
  • ImagePreviewViewController

我想从第一个 VC 中按下一个剪辑,然后打开媒体库来选择一个图像。然后将选中的图像作为 previewController 传递给第三个 VC。然后,如果我选择“完成”,我想将其传递给第一个 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
}

第二类它只是图像的选择器,所以我将图像传递给第三个 VC,这个图像成功出现在第三个 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)
}

【问题讨论】:

  • 你遇到了什么问题?
  • @V_rohit 我在将数据从第三个 VC 传递到第一个 VC 的代码中错过了什么?
  • 在 2 个 VC 中调用委托方法,您从中将照片传递给第 3 个 VC
  • @V_rohit 但在这种情况下我如何将它从第三个 VC 传递给第一个 vc?
  • 您在第 3 个 VC 中创建了协议,只需在第 1 个 VC 中调用此协议,使用协议即可将数据传递给第 1 个 VC

标签: swift delegates pass-data


【解决方案1】:

您需要在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
            }
        }

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    相关资源
    最近更新 更多