There seems to be a lot of tutorials online for this, but I cant find one that passes the value back from a TableView Controller to the first View Controller.
Here's my set up... The first ViewController has a textfield, when the user presses in it, it takes them to the second TableViewController, where they can select a country from the list. Once they have selected it, I want them to press the DONE button on the Navigation Bar, and then I want it to segue back to the first ViewController and show the Country they selected in the textfield (ready for them to press a button and the app continues etc...)
From what I can work out, I need to do this in the DidSelectRowAtIndexPath method...? But I also keep seeing talk on unwind segues? Sorry I am new to Swift and iOS code, so would appreciate a clear answer on what is the best and easiest way to do this. Please see my code below for each controller:
ViewController
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
performSegueWithIdentifier("countryTableView", sender: self)
return false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
TableViewController
import UIKit
class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tabeView: UITableView!
var countryPicker = ["Colombia", "England", "France", "Germany", "Brazil", "Austria", "Spain", "Australia", "USA", "Berlin", "Thailand", "Northern Ireland", "New Zealand", "Hawaii", "Switzerland", "Sweeden", "Finland", "Turkey", "Russia", "Netherlands", "Japan"]
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return countryPicker.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell") as! UITableViewCell
cell.textLabel!.text = countryPicker[indexPath.row]
cell.selectionStyle = UITableViewCellSelectionStyle.Blue
return cell
}
}
Thanks in advance!