gpt4 book ai didi

ios - 协议(protocol)和委托(delegate)不起作用..我做错了什么?

转载 作者:行者123 更新时间:2023-11-30 13:52:27 24 4
gpt4 key购买 nike

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

发送 VC 是:TrainersViewController接收 VC 是:BioViewController

这是我的协议(protocol):

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 中的按钮:

    @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 中?

最佳答案

当你打电话的时候

delegate?.trainersViewController(self, didFinishSendingImage: trainer)

变量delegate尚未设置。您可以使用以下行进行检查:

print(delegate)

如果未设置委托(delegate),则此行将打印 nil

关于ios - 协议(protocol)和委托(delegate)不起作用..我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34097217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com