协议和代理不工作..我做错了什么? [英] Protocol and Delegate not working.. what did i do wrong?

查看:139
本文介绍了协议和代理不工作..我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过委托和协议将我的collectionViewCell imageView中的图像发送到另一个VC中的imageView。我不知道为什么这不能正常工作?

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?

发送VC是: TrainersViewController
收到的VC是: BioViewController

这是我的协议:

protocol TrainersViewControllerDelegate: class {

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


class TrainersViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, AddNewTrainerViewControllerDelegate {

    weak var delegate: TrainersViewControllerDelegate? 
}

这是我的接收班:

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

最后,这是CollectionViewCell中的按钮,我呼吁这样做:

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

为什么这不会将我的图像发送到另一个VC的imageView?

推荐答案

只需在 prepareForSegue 中传递图像,这将是最简单的方法(但是您必须在发送类中实现它)。以前的答案是正确的。您在设置委托之前使用它。如果你真的想使用它,我会首先执行segue,当准备它时,你设置委托,然后尝试使用委托方法。

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 :)

您可以尝试撤销您的代理。在 BioViewController make BioViewControllerDelegate 与方法 bioViewControllerSetUpImage(bioViewController:BioViewController) - > UIImage 在发送类中实现它,并在BioViewController类中调用它,例如在 viewDidLoad 中,像 bioImage = delegate?.bioViewControllerSetUpImage自我)。然后创建 BioViewController ,设置委托,segue。 (但这是我试图解决它的问题,随时忽略它)

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)

这篇关于协议和代理不工作..我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆