Reputation: 2998
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 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
}
}
}
Upvotes: 1
Views: 1754
Reputation: 114846
The first step is to keep the PFObjects
that you have retrieved rather than simply extracting the country name and throwing the object away, as you will need it later.
import UIKit
import Parse
class TableViewController: UITableViewController {
var country = [PFObject]()
var pickedCountry: PFObject?
override func viewDidLoad() {
super.viewDidLoad()
var countryQuery = PFQuery(className: "country")
countryQuery.orderByAscending("name")
countryQuery.findObjectsInBackgroundWithBlock {
(countries:[AnyObject]?, error: NSError?) -> Void in
if error == nil {
self.country=countries as! [PFObject]
self.tableView.reloadData()
} else {
print(error)
}
}
}
This means that you will need to change your methods that access the data - cellForRowAtIndexPath
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let rowCountry = country[indexPath.row]
cell.textLabel!.text = rowCountry["name"]
return cell
}
Now in your original view controller when you access pickedCountry
it will be a PFObject
and you can access the various fields -
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["name"]
countryCodeLabel.text = countryChosen["code"]
countryCurrencyLabel.text = countryChosen["currency"]
}
You will need to make similar changes in the first view controller - Change the string variable to a PFObject and access the country name field
import UIKit
import Parse
var countryChosen: PFObject!
@IBAction func buttonPressed(sender: AnyObject) {
if textfield.text != nil {
performSegueWithIdentifier("done", sender: self)
}
// countryChosen = textfield.text! // You can't do this any more because you need a PFObject, not text. I am not sure you would want to anyway, because they may have typed an invalid country. Probably use a label instead of a text field
print(countryChosen)
}
@IBAction func selectedCountry (segue: UIStoryboardSegue) {
let countryTableViewController = segue.sourceViewController as! TableViewController
if let selectedCountry = countryTableViewController.pickedCountry {
textfield.text = selectedCountry["name"]
}
}
Upvotes: 2