Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

ios - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...