Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift tab bar view prepareforsegue

In VC1 (collection view) this is my prepareforsegue code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let segue = segue.destinationViewController as TabBarViewController


    var selectedIndex = self.collectionView.indexPathForCell(sender as UICollectionViewCell)

    segue.selectedIndexPassing = selectedIndex?.row

}

When I get to VC2 (which is TabBarViewController), I println() selectedIndexPassing to see what returns. It returns correctly. And then, in VC2, I call this prepareforsegue to get to the actual view controller, or the first button in the tab bar:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var segue = segue.destinationViewController as PlayerFromRosterViewController
    segue.selectedIndexPassingForDisplay = 1
}

However, when I println() the selectedIndexPassingForDisplay in VC3 (PlayerFromRosterViewController) I get nil. Why isn't the variable passing from the Tab Bar navigation controller to VC3, AKA the first tab bar button view.

like image 467
Jim McDermott Avatar asked Dec 05 '14 02:12

Jim McDermott


1 Answers

Have you checked whether the prepareForSegue in your TabBarController ever gets called?

It would never be called since the connection between a UITabBarController and its children view controllers isn't a storyboard segue. You should get a reference directly to the viewController from the UITabBarController by accessing its viewControllers collection at the index you want.

var tabBarController = segue.destination as UITabBarController
var destinationViewController = tabBarController.viewControllers[0] as YourViewController // or whatever tab index you're trying to access
destination.property = "some value"
like image 193
shim Avatar answered Nov 18 '22 07:11

shim