Swift pass data through navigation controller

IosSwiftSegue

Ios Problem Overview


One of my segues transitions from a view controller to a tableview controller. I want to pass an array between the two, but with a navigation controller before the tableview controller, I can't figure out how to pass the array. How do you pass data through a navigatiom controller to a tableview controller?

Ios Solutions


Solution 1 - Ios

Override prepareForSegue and set whatever value on the tableview you'd like. You can grab the tableview from the UINavigation viewControllers property.

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!){

   let navVC = segue.destinationViewController as UINavigationController
    
   let tableVC = navVC.viewControllers.first as YourTableViewControllerClass
    
   tableVC.yourTableViewArray = localArrayValue   
}

For Swift 3 :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

   let navVC = segue.destination as? UINavigationController
    
   let tableVC = navVC?.viewControllers.first as! YourTableViewControllerClass
    
   tableVC.yourTableViewArray = localArrayValue   
}

Solution 2 - Ios

@Tommy Devoy's answer is correct but here is the same thing in swift 3

Updated Swift 3

  // MARK: - Navigation

//In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.

    
    if segue.identifier == "yourSegueIdentifier"  {
    
        if let navController = segue.destination as? UINavigationController {
            
            if let chidVC = navController.topViewController as? YourViewController {
                 //TODO: access here chid VC  like childVC.yourTableViewArray = localArrayValue 

            
            }
            
        }
    
    }
    
}

Solution 3 - Ios

I was getting this error to Horatio's solution.

ERROR: Value of type 'UINavigationController' has no member 'yourTableViewArray'

So this might help others like me looking for a code only solution. I wanted to redirect to a Navigation controller, yet pass data to root view controller within that Nav Controller.

if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourNavigationControllerID") as? UINavigationController,
   let yourViewController = controller.viewControllers.first as? YourViewController {
        yourViewController.yourVariableName = "value"
        self.window?.rootViewController = controller  // if presented from AppDelegate
        // present(controller, animated: true, completion: nil) // if presented from ViewController
}

Solution 4 - Ios

SWIFT 3 (tested solution) - Passing data between VC - Segue with NavigationController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        let navigationContoller = segue.destination as! UINavigationController

        let receiverViewController = navigationContoller?.topViewController as ReceiverViewController

        receiverViewController.reveivedObject = sentObject 
    }

Solution 5 - Ios

Tommy's solution requires you to set up the Segue in Storyboard.

Here is a code only solution:

func functionToPassAsAction() {
    var controller: UINavigationController
    controller = self.storyboard?.instantiateViewControllerWithIdentifier("NavigationVCIdentifierFromStoryboard") as! UINavigationController 
    controller.yourTableViewArray = localArrayValue
    self.presentViewController(controller, animated: true, completion: nil)
}

Solution 6 - Ios

Swift 5

class MyNavigationController: UINavigationController {
    
    var myData: String!
    
    override func viewDidLoad() {
        if let vc = self.topViewController as? MyViewcontoller{
            vc.data = self.myData
        }
    }
    
}

   let navigation = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MyNavigationController") as! MyNavigationController
   navigation.myData = "Data that you want to pass"
   present(navigation, animated: true, completion: nil)

Solution 7 - Ios

FIXED syntax for SWIFT 3

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let navVC = segue.destination as! UINavigationController
        let tableVC = navVC.viewControllers.first as! YourTableViewControllerClass
        
        tableVC.yourTableViewArray = localArrayValue
    }

Solution 8 - Ios

passing a text in tableview swift 4 **or just pass data but change the function **

First View Controller

class

 let name_array = ["BILL GATES", "MARK ZUKERBERG", "DULKAR SALMAN", "TOVINO" , "SMITH"]

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

    let story: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    
    let new = story.instantiateViewController(withIdentifier: "SecondViewController")as! SecondViewController
     
        new.str1 = name_array[indexPath.row]
    
    self.navigationController?.pushViewController(new, animated: true)

Second View Controller

class SecondViewController: UIViewController {

@IBOutlet weak var lbl2: UILabel!

var str1 = String()

override func viewDidLoad() {
    super.viewDidLoad()

     lbl2.text =  str1
  
}
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3142972View Question on Stackoverflow
Solution 1 - IosTommy DevoyView Answer on Stackoverflow
Solution 2 - IosIndrajit Sinh RayjadaView Answer on Stackoverflow
Solution 3 - IosmythicalcoderView Answer on Stackoverflow
Solution 4 - IosLamaToView Answer on Stackoverflow
Solution 5 - IosHoratioView Answer on Stackoverflow
Solution 6 - IosRashid LatifView Answer on Stackoverflow
Solution 7 - IosMANISH PATHAKView Answer on Stackoverflow
Solution 8 - IosFaizal Nowshad KNView Answer on Stackoverflow