Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepare for Segue to UITabBarController Tab

I want to pass information from a ViewController to a UITabBarController so that I can access certain information from the view controller in the 3rd tab of the UITabBarController.

However, the issue is my program keeps crashing when I try to do so. Here is my code so far:

I call the segue like so:

self.firstName = user.first_name
self.lastName = user.last_name
self.performSegueWithIdentifier("OffersView", sender: self)

And I override the prepareForSegue function so that I can pass information in this function like so:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "OffersView"){
        let barViewControllers = segue.destinationViewController as UITabBarController
        let destinationViewController = barViewControllers.viewControllers![2] as ProfileController
        destinationViewController.firstName = self.firstName
        destinationViewController.lastName = self.lastName
    }
}

When I try to set the destinationViewController (on the second line in the code above), my code crashes. I'm not sure why as I've looked on numerous StackOverflow posts such as

Swift tab bar view prepareforsegue and Pass data from tableview to tab bar view controller in Swift. but have not have much success. Do I possibly need to create a UITabBarControllerDelegate class and pass information through that? Any tips would be appreciated. Thanks!

like image 523
user1871869 Avatar asked Apr 11 '15 00:04

user1871869


3 Answers

The problem, as discovered after a discussion, was that the tab bar controller's children were embedded in navigation controllers, so the code needed to be changed to this,

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    let barViewControllers = segue.destinationViewController as! UITabBarController 
    let nav = barViewControllers.viewControllers![2] as! UINavigationController 
    let destinationViewController = nav.topviewcontroller as ProfileController 
    destinationViewController.firstName = self.firstName
    destinationViewController.lastName = self.lastName 
}
like image 173
rdelmar Avatar answered Nov 18 '22 12:11

rdelmar


In swift 3, xcode 8 that code would be like

let barViewControllers = segue.destination as! UITabBarController
let nav = barViewControllers.viewControllers![0] as! UINavigationController
let destinationViewController = nav.viewControllers[0] as! YourViewController
        destinationViewController.varTest = _varValue
like image 27
Jorge Manuel Bello Avatar answered Nov 18 '22 10:11

Jorge Manuel Bello


Swift 4 solution without force optional unwrapping

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let barVC = segue.destination as? UITabBarController {
        barVC.viewControllers?.forEach {
            if let vc = $0 as? YourViewController {
                vc.firstName = self.firstName
                vc.lastName = self.lastName
            }
        }
    }
}
like image 2
Mario Steingruber Avatar answered Nov 18 '22 11:11

Mario Steingruber