I have set up a small test database in Parse which you can see below:
There is a country 'name', country 'code' and country 'currency'
So the main context of the app is this:
- On the first screen, there is a textfield, which if they press, it takes them to a tableview controller and populates with the list of countries from Parse (the 6 you see above - this all works fine)
- When they select a country, it takes them back to the first VC and saves the country to a variable, of type String. Called 'countryChosen' (this works fine)
- When they press the Confirm button, it takes them to a ResultsVC. It has 3 labels on it, one for the name of the country they chose in the tableView, and then one for the 'code', and one for the 'currency' of that selected country. See the layout below:
On this screen, currently, i have updated the top label with the country name no problem. I used this code:
import UIKit
import Parse
class ResultsViewController: UIViewController {
@IBOutlet weak var countryChosenLabel: UILabel!
@IBOutlet weak var countryCodeLabel: UILabel!
@IBOutlet weak var countryCurrencyLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
countryChosenLabel.text = countryChosen
}
What I'm struggling with
I need to populate the other 2 labels with the correct data from Parse. So for example, if the user selected the country United Kingdom, then when they get to the ResultsVC, it could show: United Kingdom, GB, GBP.
I don't know how to make the query to Parse to ask for this information. I'm very new to using Parse so any help would really help!
Thanks very much.
UPDATED Code from my TableViewController.swift
import UIKit
import Parse
class TableViewController: UITableViewController {
var country = [String]()
var pickedCountry: String?
override func viewDidLoad() {
super.viewDidLoad()
var countryQuery = PFQuery(className: "country")
countryQuery.orderByAscending("name")
countryQuery.findObjectsInBackgroundWithBlock {
(countryName:[AnyObject]?, error: NSError?) -> Void in
if error == nil {
for name in countryName! {
self.country.append(name["name"] as! String)
}
self.tableView.reloadData()
} else {
print(error)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return country.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel!.text = country[indexPath.row]
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "pickedCountry" {
let cell = sender as! UITableViewCell
let index = tableView.indexPathForCell(cell)
if let indexPath = index?.row {
pickedCountry = country[indexPath]
}
}
}
}
First ViewController.swift
import UIKit
import Parse
var countryChosen: String!
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textfield: UITextField!
@IBOutlet weak var button: UIButton!
@IBAction func TFPressed(sender: AnyObject) {
performSegueWithIdentifier("toTable", sender: self)
}
@IBAction func buttonPressed(sender: AnyObject) {
if textfield.text != nil {
performSegueWithIdentifier("done", sender: self)
}
countryChosen = textfield.text!
print(countryChosen)
}
@IBAction func selectedCountry (segue: UIStoryboardSegue) {
let countryTableViewController = segue.sourceViewController as! TableViewController
if let selectedCountry = countryTableViewController.pickedCountry {
textfield.text = selectedCountry
}
}
}
