一尘不染

快捷标签栏视图prepareforsegue

swift

在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
}

但是,当我selectedIndexPassingForDisplay在VC3(PlayerFromRosterViewController)中使用println()时,结果为零。为什么变量不从Tab
Bar导航控制器传递到VC3,又是第一个Tab Bar按钮视图。


阅读 243

收藏
2020-07-07

共1个答案

一尘不染

您是否检查prepareForSegue过TabBarController中的是否曾经被调用?

永远不会调用它,因为a
UITabBarController及其子视图控制器之间的连接不是情节提要。您应该UITabBarController通过viewControllers在所需索引处访问viewController的集合来直接从viewController获得对其的引用。

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"
2020-07-07