ios - Retrieve object data from Parse in Swift -


i have set small test database in parse can see below:

enter image description here there country 'name', country 'code' , country 'currency'

so main context of app this:

  1. on first screen, there textfield, if press, takes them tableview controller , populates list of countries parse (the 6 see above - works fine)
  2. when select country, takes them first vc , saves country variable, of type string. called 'countrychosen' (this works fine)
  3. when press confirm button, takes them resultsvc. has 3 labels on it, 1 name of country chose in tableview, , 1 'code', , 1 'currency' of selected country. see layout below:

enter image description here

on screen, currently, have updated top label country name no problem. used 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 populate other 2 labels correct data parse. example, if user selected country united kingdom, when resultsvc, show: united kingdom, gb, gbp.

i don't know how make query parse ask information. i'm new using parse help!

thanks much.

updated code 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 {              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      }  } } 

the first step keep pfobjects have retrieved rather extracting country name , throwing object away, need 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 need change methods access 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 original view controller when access pickedcountry pfobject , can access 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 need make similar changes in first view controller - change string variable pfobject , access 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!  // can't more because need pfobject, not text.  not sure want anyway, because may have typed invalid country.  use label instead of text field      print(countrychosen) }  @ibaction func selectedcountry (segue: uistoryboardsegue) {      let countrytableviewcontroller = segue.sourceviewcontroller as! tableviewcontroller      if let selectedcountry = countrytableviewcontroller.pickedcountry {         textfield.text = selectedcountry["name"]     } } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -