Swift选项卡视图prepareforsegue

7
在VC1(集合视图)中,这是我的prepareforsegue代码:
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

}

当我到达VC2(即TabBarViewController)时,我使用println()函数来查看返回的selectedIndexPassing值。它返回正确的值。然后,在VC2中,我调用prepareforsegue函数以进入实际的视图控制器或选项卡栏中的第一个按钮:

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

然而,当我在VC3(PlayerFromRosterViewController)中println() selectedIndexPassingForDisplay 时,它是nil。为什么这个变量没有从选项卡导航控制器传递到VC3,也就是第一个选项卡按钮视图。

1个回答

22

你是否检查过 TabBarController 中的 prepareForSegue 是否被调用了?

它不会被调用,因为 UITabBarController 与其子视图控制器之间的连接不是故事板segue。你应该直接从 UITabBarController 中访问其 viewControllers 集合中所需索引的视图控制器来获取引用。

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"

1
明白了...你能快速展示一下怎么做吗?只需要一行代码的样例就好。谢谢! - Jim McDermott
1
谢谢!我现在明白了。 - Jim McDermott
1
难以置信,我从来没有想过它没有“真正”的segue。你为我节省了第二个小时的尝试。谢谢。 - djdance
1
你是最棒的 :) - Mariam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接