Swift pass data through navigation controller

后端 未结 8 2114
青春惊慌失措
青春惊慌失措 2020-12-12 18:13

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

相关标签:
8条回答
  • 2020-12-12 18:19

    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)
    
    0 讨论(0)
  • 2020-12-12 18:24

    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   
    }
    
    0 讨论(0)
  • 2020-12-12 18:32

    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)
    }
    
    0 讨论(0)
  • 2020-12-12 18:33

    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
    
    }
    
    0 讨论(0)
  • 2020-12-12 18:34

    @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 
    
    
                }
    
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-12 18:35

    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 
        }
    
    0 讨论(0)
提交回复
热议问题