Search code examples
iosswiftxcode6.3.1

How to back to previous view controller with parameters in swift


TableViewController B shows up when a button in ViewController A is tapped. A user selects an item in B and then is led back to A. I used the following code to go back to A from B. But how can I pass the selected item ([Int: String]) to A?

navigationController?.popViewControllerAnimated(true)

Below is the code to navigate to B from A

 let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

 let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("categories") as! CategoryListViewController

 self.presentViewController(nextViewController, animated:true, completion:nil)

Solution

  • In your ViewController A class create an empty Dictionary like this

    var dict = [Int: String]()    
    

    Now when you move from TableViewController B to ViewController A set the value to dict like this

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    
     let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("categories") as! CategoryListViewController
    
    // here you pass parameter to ViewController A
    
    nextViewController.dict =  YourVaribleWhichStrore[Int:String] 
    
     self.presentViewController(nextViewController, animated:true, completion:nil)