如何在 segue 期间将数据传递给 2 个或更多 ViewController

How do I pass data to 2 or more ViewControllers during segue

我正在尝试将数据从 SwipeCardVC 传递到 CardDetailVC,它是嵌入不同容器(即 ReadReviewVC)的多个子视图控制器的父级。

虽然我可以毫无问题地将数据传递给父 CardDetailVC,但子视图控制器也需要访问我传递的数据。

到目前为止,我已经尝试在我的 prepare for segue 代码中添加一个额外的目的地,如下所示:

更新:Storyboard Screenshot

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toDetailVC" {
        let cardDetailVC = segue.destination as? CardDetailVC
        cardDetailVC?.card = CardController.shared.topCard
        let readReviewVC = segue.destination as? ReadReviewsVC
        readReviewVC?.card = CardController.shared.topCard
    }

// UPDATED CODE - CustomViewController
class CardViewController: UIViewController {
    var card: Card?
}

// UPDATED CODE - ParentViewController
class ParentCardDetailVC: CardViewController {

    private(set) lazy var childrenVCs: [CardViewController] = {
        return [
            UIStoryboard(name: "Detail", bundle: nil).instantiateViewController(withIdentifier: "cardDetailVC"),
            UIStoryboard(name: "Detail", bundle: nil).instantiateViewController(withIdentifier: "readReviewsVC")]
    }() as! [CardViewController]

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        for child in childrenVCs {
            child.card = self.card
        }
    }
}

// UPDATE ChildVC
class CardDetailVC: CardViewController {

    // MARK: - Properties
    override var card: Card? {
        didSet {
            updateViews()
        }
    }
.
.
.
    // MARK: - Methods
    private func updateViews() {
        guard let card = card else { print("No card object found in \(#function)") ; return }

        cardImageView.image = card.image
        foodNameLabel.text = card.foodName
        priceLabel.text = card.price
        restaurantLabel.text = card.restaurantName
        descriptionLabel.text = card.description
        streetLabel.text = card.street
        cityStateLabel.text = card.cityState
    }
}

虽然 cardDetailVC.card 值是正确的,但 readReviewVC.card 值总是返回 nil,让我相信每个标识符只能有 1 个目的地。请帮助重构此代码或让我知道是否有不同的方法来完成此任务(即:NSNotifications 或 KVO)。

你不仅不能继续到 2 UIViewControllers,你的 parent VC 应该有数据,然后应该 委托 到 children。我轻描淡写地说委托是因为这与 Swift 中的 delegate 方法不同。

应该会发生这样的事情。

ParentVC 包含一个数组 childrenVCs:[CustomViewController]。因此 viewWillAppear() 遍历 childrenVCs 并为他们提供数据。

class CustomViewController: UIViewController {
    var card:Int!

    //...
}

class CardDetailVC: CustomViewController {
    //...
}

class ReadReviewsVC: CustomViewController {
    //...
}

class ParentVC: CustomViewController {

    //Setup Children ViewControllers inside code or inside storyboard
    var childrenViewControllers:[CustomViewController] = [//...]

    func viewWillAppear() {
        for children in childrenViewControllers {
            children.card = self.card
        }
    }
}

class SegueFromController:UIViewController {
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "toDetailVC" {
            let cardDetailVC = segue.destination as? ParentVC
            cardDetailVC?.card = CardController.shared.topCard
        }
    }
}