2016-11-07 114 views
0

這是我在Firebase here中創建的表格。我有一個搜索按鈕。該按鈕動作首先會從Firebase中獲取數據,然後將其發送到另一個視圖控制器並將其顯示在表格視圖中。但主要的問題是,從火力地堡從Firebase中檢索數據後,如何將檢索到的數據從一個視圖控制器發送到另一個視圖控制器?

self.performSegueWithIdentifier("SearchResultPage", sender: self) 

觸發器和我的下一個視圖控制器獲取的所有數據之前顯示空表視圖。這是我的努力here

來自這篇文章here我認爲我的代碼放置不好。

回答

1

寫這行代碼在你的數據傳遞方法,如果塊之後完成循環

self.performSegueWithIdentifier("SearchResultPage", sender: self) 
+0

不work.Please從這篇文章仔細閱讀後推到下一個視圖控制器。首先,您必須完成從Firebase的所有操作。那麼你有觸發self.peformse .....()http://stackoverflow.com/questions/37780207/good-way-to-retrieve-information-from-firebase-swift – KhanShaheb

0

嘗試發送的搜索字符串作爲發件人,當你執行SEGUE,例如:

self.performSegueWithIdentifier("SearchResultPage", sender: "searchString") 

然後,在準備segue的過程中,獲取目標視圖控制器並將字符串發送到新的視圖控制器。是這樣的:

destinationViewController.searchString = sender as! String 

當SEGUE完成,你已經來到了新的視圖控制器,你現在應該可以設置一個搜索值,使用該值來進行查詢並獲取相關數據顯示在新的表格視圖中。

請注意,這只是衆多解決方案之一,還有其他方法可以實現這一點。

+0

謝謝。我在你的解決方案之前做到了。但我需要預期的解決方案。首先它將獲取數據,然後self.performSegueWithIdentifier()將被觸發。 – KhanShaheb

+0

@KhanShaheb哦,好的,那麼你會用類似的方法,但是你會得到所有的數據並把它作爲一個字典,然後你在performSegue中傳遞字典作爲發送者,然後在prepareForSegue中傳遞整個字典將結果提交給下一個視圖控制器:) – Wink

+0

在獲取數據的時候..... self.performSegueWithIdentifier()開始。那就是爲什麼不行。我需要這種類型的閉包,或者在獲取數據的時候一切都會變得模糊,並且在完成加載之後,一切都將開始。 – KhanShaheb

0
  • 您無法發送數據的原因是因爲您甚至在數據被實際提取之前嘗試發送數據。
  • 請嘗試以下,你只得到數據

    func dataTransfer() { 
    
    let BASE_URL_HotelLocation = "https://**************.firebaseio.com/Niloy" 
    
    let ref = FIRDatabase.database().referenceFromURL(BASE_URL_HotelLocation) 
    
    ref.queryOrderedByChild("location").queryStartingAtValue("uttara").observeEventType(.Value, withBlock: { snapshot in 
    
        if let result = snapshot.children.allObjects as? [FIRDataSnapshot] { 
    
         for child in result { 
    
          let downloadURL = child.value!["image"] as! String; 
    
          self.storage.referenceForURL(downloadURL).dataWithMaxSize(25 * 1024 * 1024, completion: { (data, error) -> Void in 
           let downloadImage = UIImage(data: data!) 
    
           let h = Hotel() 
    
    
           h.name = child.value!["name"] as! String 
           print("Object \(count) : ",h.name) 
           h.deal = child.value!["deal"] as! String 
           h.description = child.value!["description"] as! String 
           h.distance = child.value!["distance"] as! String 
           h.latestBooking = child.value!["latestBooking"] as! String 
           h.location = child.value!["location"] as! String 
           h.image = downloadImage! 
    
           self.HotelObjectArray.append(h) 
          }) 
    
         } 
          let storyboard = UIStoryboard(name:"yourStoryboardName", bundle: nil) 
          let vc = storyboard.instantiateViewControllerWithIdentifier("SearchResultPageIdentifier") as! SearchResultPage       
          self.navigationController.pushViewController(vc, animated: true) 
    
        } 
        else { 
         print("no results") 
        } 
    
        }) 
    

    }

相關問題