-1

I added the ImageView Protocol. What can be done to remove the error

Do you want to add protocol stubs?

CardsViewController

import UIKit

protocol ImageViewProtocol{ 
    func sendImageToViewController(theImage: UIImage) 
}

class CardsViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, ImageViewProtocol {
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var locationTextField: UITextField!
    @IBOutlet weak var imageView: UIImageView!

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

    func chooseImagePickerAction(source: UIImagePickerController.SourceType) {
        if UIImagePickerController.isSourceTypeAvailable(source) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.allowsEditing = true
            imagePicker.sourceType = source
            self.present(imagePicker, animated: true, completion: nil)
        }
    }

    @IBAction func saveButtonPressed(_ sender: UIBarButtonItem) {
        if nameTextField.text == "" || locationTextField.text == "" || textField.text == "" {
            print("Not all fields are filled")
        } else {
            if let context = (UIApplication.shared.delegate as? AppDelegate)?.coreDataStack.persistentContainer.viewContext {
                let card = Card(context: context)
                card.name = nameTextField.text
                card.location = locationTextField.text
                card.number = textField.text
                if let image = imageView.image {
                    card.image = image.pngData()
                }
                do {
                    try context.save()
                    print("Cохранение удалось!")
                } catch let error as NSError {
                    print("Не удалось сохранить данные \(error), \(error.userInfo)")
                }
            }

            performSegue(withIdentifier: "unwindSegueFromNewCard", sender: self)
        }
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        imageView.image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        dismiss(animated: true, completion: nil)
    }
}

ViewController2

import UIKit

class ViewController2: UIViewController {
    var filter : CIFilter!
    var delegate: ImageViewProtocol!

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

    override func viewDidLoad() {
        super.viewDidLoad()

        barcodeImageView.image = UIImage(named: "photo")
    }

    @IBAction func saveButtonAction(_ sender: Any) {
        if textField.text == "" {
            print("Not all fields are filled")
        } else {
            delegate.sendImageToViewController(theImage: barcodeImageView.image!)
            self.navigationController?.popViewController(animated: true)
        }
        performSegue(withIdentifier: "unwindSegueFromViewController", sender: sender)
    }

    @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))
                barcodeImageView.image = image
            }
        }
    }
}
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Day Man
  • 33
  • 1
  • 7

1 Answers1

1

This error comes because you implemented protocol (ImageViewProtcol) but you haven't add required methods of your protocol (in your case sendImageToViewController(theImage: UIImage)). All methods of your protocol are required by default. If you want to change it, you can look here.

It's the same as when you're implementing UITableViewDataSource, you also need to add required methods like number of items etc.

To fix this, add this method to your CardsViewController:

func sendImageToViewController(theImage: UIImage) {
    // do something with image
}
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40