使用委托将数据从 xib 传递到 VC

Passing data using delegates from xib to VC

我正在尝试将数据从 xib 向上传递到 VC 中,这样我就可以导航到一个单独的视图。我确实在 IGListKit 部分中获取了数据,但由于某种原因 passDataFromSectionUp 函数不会触发 VC.

中的 print("like") 函数

VC

class MainViewController: UIViewController, PassSectionDelegate {

func passDataFromSectionUp(sectionController: ExperiencesSectionController) {
    print("Like"); //Doesn't trigger
}

IGListKit 部分

protocol PassSectionDelegate: class {
  func passDataFromSectionUp(sectionController: ExperiencesSectionController);
}

class ExperiencesSectionController: ListSectionController, UpdateDataDelegate {

weak var delegate: PassSectionDelegate? = nil;

func passUpdateData(data: String) {
    print(data) //Data gets received
    delegate?.passDataFromSectionUp(sectionController: self);
}

...

if let cell = cell as? CarouselControlCell {
        cell.delegate = self;
    }

西布

protocol UpdateDataDelegate: class {
  func passUpdateData(data: String);
}

class CarouselControlCell: UICollectionViewCell {

weak var delegate: UpdateDataDelegate? = nil

@IBAction func addUpdate(_ sender: Any) {
    delegate?.passUpdateData(data: "teeest");
}

您的 ExperiencesSectionController class 中的 delegate: PassSectionDelegate 变量设置为 nil,并且根据您共享的代码从未更新过。当调用 delegate?.passDataFromSectionUp(sectionController: self) 时,delegate 变量将因此被识别为 nil,随后将不执行任何操作。