Protocol and Delegate not working.. what did i do

2019-09-08 08:39发布

问题:

i am trying to send an image from my collectionViewCell imageView to an imageView in another VC through delegation and Protocol. I cannot figure out why this isn't working properly?

The sending VC is: TrainersViewController The recieving VC is: BioViewController

Here is my protocol:

protocol TrainersViewControllerDelegate: class {

    func trainersViewController(controller: TrainersViewController, didFinishSendingImage trainer:TrainerArray)
}


class TrainersViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, AddNewTrainerViewControllerDelegate {

    weak var delegate: TrainersViewControllerDelegate? 
}

Here is my Receiving class:

class BioViewController: UIViewController, TrainersViewControllerDelegate {


    @IBAction func backToTrainersButton(sender: AnyObject) {

        dismissViewControllerAnimated(true, completion: nil)
    }

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

    @IBOutlet weak var bioImage: UIImageView!


    func trainersViewController(controller: TrainersViewController, didFinishSendingImage trainer: TrainerArray) {

        trainer.trainerImage! = bioImage as! UIImage
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "bioSegue" {
            let navigationController = segue.destinationViewController as! UINavigationController
            let controller = navigationController.topViewController as! TrainersViewController
            controller.delegate = self
        }
    }
}

Finally here is the button within the collectionViewCell that i am calling upon to make this happen:

    @IBAction func bioSegueButton(sender: AnyObject) {
    let index = sender.tag
    let trainer = trainers[index]
    print(trainer.name)
    delegate?.trainersViewController(self, didFinishSendingImage: trainer)
    performSegueWithIdentifier("bioSegue", sender: self)
}

Why is this not sending my image across into the imageView of the other VC?

回答1:

At the time when you call

delegate?.trainersViewController(self, didFinishSendingImage: trainer)

the variable delegate isn't set yet. You can check this with the following line:

print(delegate)

If the delegate isn't set, then this line will print nil



回答2:

You can just pass image in prepareForSegue and that would be simplest way (but you have to implement it in sending class). The previous answer is correct. You use it before you set delegate. If you really want to use it i would first perform the segue where when preparing for it you set the delegate and then try to use delegate method.

@IBAction func bioSegueButton(sender: AnyObject) {
    let index = sender.tag
    let trainer = trainers[index]
    print(trainer.name)
    performSegueWithIdentifier("bioSegue", sender: self)
    delegate?.trainersViewController(self, didFinishSendingImage: trainer)
}

It could cause another problem but it would be not about delegation :)

You could try reversing your delegation. In BioViewController make BioViewControllerDelegate with method bioViewControllerSetUpImage(bioViewController: BioViewController) -> UIImage implement it in sending class and call it in BioViewController class for example in viewDidLoad like bioImage = delegate?.bioViewControllerSetUpImage(self). Then you create BioViewController, set delegate, segue. (But this is my attempt to work it around, feel free to ignore it)