我在Parse中设置了一个小型测试数据库,您可以在下面看到:
有一个国家/地区' 名称',国家/地区' 代码'和国家/地区' 货币'
因此应用程序的主要背景是:
在此屏幕上,目前,我已更新顶部标签,国家名称没有问题。我用了这段代码:
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
}
我正在努力解决
我需要使用Parse中的正确数据填充其他2个标签。因此,例如,如果用户选择了英国国家,那么当他们到达ResultsVC时,它可以显示:英国,GB,GBP。
我不知道如何向Parse提出查询以询问此信息。我使用Parse非常新,所以任何帮助都会有所帮助!
非常感谢。
已更新 我的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]
}
}
}
}
首先是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
}
}
}
答案 0 :(得分:2)
第一步是保留您检索到的PFObjects
,而不是简单地提取国家/地区名称并将对象扔掉,因为您稍后会需要它。
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)
}
}
}
这意味着您需要更改访问数据的方法 - 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
}
当您访问pickedCountry
时,现在在原始视图控制器中,它将是PFObject
,您可以访问各个字段 -
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"]
}
您需要在第一个视图控制器中进行类似的更改 - 将字符串变量更改为PFObject并访问国家/地区名称字段
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"]
}
}