0

I've tried passing data backward from my unwind segue in a number of ways. 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. The unwind segue is working, and below I use prepare for segue with some success to change the title of the previous view controller to 'new title', but the last line isn't setting nbaRotoHome.player to '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"


    }

Back in my previous view controller I have

@IBAction func prepareForUnwind(segue: UIStoryboardSegue) {


}

And after looking at this question Passing data with unwind segue I've also tried getting the data this way in the previous view controller

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

If I need to add more detail to what I'm trying to do please ask and I will edit. I wanted to ask the question but I am having trouble formulating.

Community
  • 1
  • 1
Louis Sankey
  • 481
  • 8
  • 26

1 Answers1

1

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.

In an unwind segue you are returning to an already created viewController, so viewDidLoad happened ages ago before you segued to the other viewController.


If you're using segues, you should not be mucking with the array of viewControllers in the navigationController or calling dismiss. The unwind segue will do all of that. Just get the destination in prepare(for:sender:) and set the data:

if segue.identifier == "unwindToViewController1" {

    let nbaRotoHome = segue.destination as! NBARotoHome

    nbaRotoHome.player = "new player name"
}

or in your prepareForUnwind get the source and read the data:

In this line you are missing .source. Change:

if let sourceViewController = segue as? BuyStats

to:

if let sourceViewController = segue.source as? BuyStats
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • Thanks for the reply. I'm still having trouble however. If viewDidLoad() has already been called then how does it get updated with the new data? Say I've already set a label in viewDidLoad() and now I want to change it on unwind. Isn't there something else that needs to happen (like calling viewDidLoad() again), or is reassigning the variable all you need to do? I've tried that and not having any luck. – Louis Sankey Apr 27 '17 at 19:55
  • Put the code that updates the label into its own method. Call that method from viewDidLoad() and from your unwind landing spot which you called prepareForUnwind. – vacawama Apr 27 '17 at 21:13