2015-11-03 57 views
0

我正在嘗試從Parse表中查詢數據以將其添加到prepareForSegue函數中。但是一旦進入newViewController,標籤是空白的。這是我的代碼行。查詢數據以將數據添加到prepareForSegue函數

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if(segue.identifier == "marathonDetail"){ 

     var upcoming: marathonDetailViewController = segue.destinationViewController as! marathonDetailViewController 

     let indexPath = self.marathonsTableView.indexPathForSelectedRow! 



     let currentCell = marathonsTableView.cellForRowAtIndexPath(indexPath) as! marathonTableViewCell 

     let marathonEvents = currentCell.marathonName.text 


     upcoming.nameMarathon = marathonEvents 

     self.marathonsTableView.deselectRowAtIndexPath(indexPath, animated: true) 

     var query = PFQuery(className: "marathons") 
     query.whereKey("marathonName", equalTo: marathonEvents!) 
     query.findObjectsInBackgroundWithBlock{ 
      (marathonPickeds: [PFObject]?, error: NSError?) -> Void in 
      if (error == nil){ 
       if let marathonPicked = marathonPickeds as? [PFObject]?{ 
        for marathonPicked in marathonPickeds!{ 

       var selectedDescription = marathonPicked.description 

       upcoming.marathonDescription = selectedDescription 
         print(selectedDescription) 
        } 

       } 


      }else { 

      print(error) 

      } 



     } 


    } 
} 

的marathonsEvents = currentCell.marathonName.text效果很好,但marathonDescription是空白。

有什麼建議嗎?我正在使用Parse作爲我的後端XCODE 7,並且迅速

回答

1

您正在後臺線程上執行網絡調用,以便在完成時您已完成了繼續。你可能想要做的是:

  1. 將查詢邏輯拉出到一個單獨的類,讓它從你的視圖控制器。
  2. 在此實例中,在正推送到的視圖控制器中執行請求。您可以從viewWillAppear開始,並在完成後刷新您的視圖。它看起來像使用marathonEvents來獲得執行請求所需的所有信息。
+0

我該如何刷新viewController? – Ace

+0

請求完成後,您將設置標籤的文本。 –

+0

好的,我必須改變代碼行var selectedDescription = wildCardPicked.description,因爲它給了我所有的信息,我把它改爲var selectedDescription = wildCardPicked.information,但現在我得到一個錯誤「PFObject的值類型沒有成員'信息'「我如何獲得信息欄中的信息? – Ace