收到错误消息`是否要添加协议存根?

时间:2018-12-01 20:03:52

标签: ios swift xcode protocols viewcontroller

我添加了ImageView协议。该怎么做才能消除错误

  

您要添加协议存根吗?

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
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

出现此错误是因为您实现了协议(ImageViewProtcol),但尚未添加协议的必需方法(在您的情况下为sendImageToViewController(theImage: UIImage))。默认情况下,协议的所有方法都是必需的。如果要更改它,可以查看here

与实现UITableViewDataSource时相同,还需要添加所需的方法,例如项数等。

要解决此问题,请将此方法添加到您的CardsViewController

func sendImageToViewController(theImage: UIImage) {
    // do something with image
}