映像不是从一个VC到另一个VC

时间:2018-11-29 20:18:17

标签: ios swift xcode tableview

该映像不是从一个VC到另一个VC。

问题是将其显示到主UIViewController中的图像视图。我已将所有内容正确连接到情节提要中。

单击here查看我的故事板布局。

请注意,我的一些不必要的代码已从两个ViewController类中删除。

这是我的第一个UIViewController:

import UIKit

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

    var imagePicker: UIImagePickerController!

    @IBAction func editPhoto(_ sender: UIButton) {
        let ViewController2 = storyboard?.instantiateViewController(withIdentifier: "AddNewEaterySegue") as! ViewController2
        ViewController2.imageForEdit = imageView.image!
        print(imageView)
        print(imageView.image!)
        navigationController?.pushViewController(ViewController2, animated: true)
    }

    @IBOutlet weak var imageView: UIImageView! {
        didSet {
            imageView.contentMode = .scaleAspectFill
            imageView.clipsToBounds = true
        }
    }

}

这是我的第二个UIViewController:

import UIKit

class ViewController2:  UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

    var filter : CIFilter!
    var imageForEdit = UIImage()
    var imagePicker: UIImagePickerController!

    @IBOutlet weak var select: UISegmentedControl!
    @IBOutlet weak var imageLabel: UIImageView!
    @IBOutlet weak var textField: UITextField!

    @IBAction func saveButtonPressed(_ sender: UIImage) {
        if textField.text == "" {
            print("Не все поля заполнены")
        } else {


        }
        performSegue(withIdentifier: "unwindSegueFromViewController", sender: sender)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        imageLabel.image = imageForEdit
        print(imageLabel)
        // Do any additional setup after loading the view.
    }


    @IBAction func tappedEnter(_ sender: Any) {

        if textField.text?.isEmpty ?? true {
            return
        } else {
            if let texttxt = textField.text {

                let data = texttxt.data(using: .ascii, allowLossyConversion: false)

                if select.selectedSegmentIndex == 0
                {
                    filter = CIFilter(name: "CICode128BarcodeGenerator")
                } else {
                    filter = CIFilter(name: "CIQRCodeGenerator")
                }

                filter.setValue(data, forKey: "inputMessage")
                let transform = CGAffineTransform(scaleX: 5, y: 5)
                let image = UIImage(ciImage: filter.outputImage!.transformed(by: transform))
                imageLabel.image = image
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

当您推送到ViewController2时,ViewController中的imageView为空。

您在哪里为imageView设置图像?

在情节提要中,ViewController中的imageView为空,并且在ViewController中imageView的didSet方法中,您尚未将任何图像设置为该imageView。

在ViewController中尝试一下:

 @IBOutlet weak var imageView: UIImageView! {
    didSet {

        //Add a test image to your project.
        imageView.image = UIImage(named: "YOUR_TEST_IMAGE_NAME.jpeg")
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true

    }
}

编辑:好的,我想我不清楚您的问题。因此,为了将图像从“ ViewController2”发送回“ ViewController”,您可以使用许多方法,但我要做的方法是使用协议。

我已经以一种通用的方式完成了该操作,但是您可以应用此方法将任何类型的数据从一个视图控制器发送回上一个视图控制器。

在ViewController中尝试一下:

import UIKit

protocol ImageViewProtocol{
     func sendImageToViewController(theImage: UIImage)
}

class ViewController: UIViewController, ImageViewProtocol {

    func sendImageToViewController(theImage: UIImage) {
        imageView.image = theImage
    }

    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let viewcontroller2 = storyboard?.instantiateViewController(withIdentifier: "viewcontroller2") as! ViewController2
        viewcontroller2.delegate = self
        self.navigationController?.pushViewController(viewcontroller2, animated: true)

    }

}

在ViewController2中尝试一下:

import UIKit

class ViewController2: UIViewController {

    var delegate: ImageViewProtocol!

    @IBOutlet weak var barcodeImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        barcodeImageView.image = UIImage(named: "test_image.jpg")

    }

    @IBAction func saveButtonAction(_ sender: Any) {
        delegate.sendImageToViewController(theImage: barcodeImageView.image!)
        self.navigationController?.popViewController(animated: true)
    }

}