Swift - Unwind Segue Pass 数据混乱

Swift - Unwind Segue Pass Data Confusion

我已经尝试以多种方式从我的展开转场向后传递数据。似乎数据未发送或在 viewDidLoad() 之后发送,因此我尝试设置的标签未更新。 unwind segue 正在工作,下面我使用 prepare for segue 成功地将前一个视图控制器的标题更改为 'new title',但最后一行没有将 nbaRotoHome.player 设置为 'new player name'.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "BuyStatsTapPager" {
        let nav = segue.destination as! UINavigationController


        let buyStatsTapPager = nav.viewControllers[0] as! BuyStatsTabPager
            buyStatsTapPager.selectedPlayerBuyStats = selectedPlayer
            buyStatsTapPager.buyStatsRef = self

    }

    if segue.identifier == "unwindToViewController1" {

        var viewControllers: [UIViewController] = mainNavigationController.viewControllers as [UIViewController];

        if(viewControllers.count == 3){
            viewControllers.remove(at: viewControllers.count-2)
            mainNavigationController?.viewControllers = viewControllers
        }

        let enteredContestViewController = viewControllers[viewControllers.count-1]
        enteredContestViewController.title = "new title"

        self.presentingViewController?.dismiss(animated: true, completion: nil)

        let nbaRotoHome = segue.destination as! NBARotoHome

        nbaRotoHome.player = "new player name"


    }

回到我以前的视图控制器,我有

@IBAction func prepareForUnwind(segue: UIStoryboardSegue) {


}

看了这个问题之后 我也试过在以前的视图控制器中以这种方式获取数据

 @IBAction func prepareForUnwind(segue: UIStoryboardSegue) {
    if let sourceViewController = segue as? BuyStats {
        playerNameLabel.text = sourceViewController.playerName
    }
}

如果我需要为我正在尝试做的事情添加更多细节,请询问,我会进行编辑。我想问这个问题,但我无法表述。

It seems like the data is not getting sent or its getting sent after viewDidLoad() so the label I'm trying to set isn't getting updated.

unwind segue 中,您将返回到一个已经创建的 viewController,因此 viewDidLoad 发生在很久以前,然后您才转到另一个 [=13] =].


如果您正在使用 segues,则不应在 navigationController 中处理 viewController 数组或调用 dismissunwind segue 将完成所有这些。只需在 prepare(for:sender:) 中获取 destination 并设置数据:

if segue.identifier == "unwindToViewController1" {

    let nbaRotoHome = segue.destination as! NBARotoHome

    nbaRotoHome.player = "new player name"
}

或在您的 prepareForUnwind 中获取 来源 并读取数据:

您在这一行中遗漏了 .source。变化:

if let sourceViewController = segue as? BuyStats

至:

if let sourceViewController = segue.source as? BuyStats