从Firebase中检索数据后,如何将检索到的数据从一个视图控制器发送到另一个视图控制器?

从Firebase中检索数据后,如何将检索到的数据从一个视图控制器发送到另一个视图控制器?

问题描述:

这是我在Firebase here中创建的表格。我有一个搜索按钮。该按钮动作首先会从Firebase中获取数据,然后将其发送到另一个视图控制器并将其显示在表格视图中。但主要的问题是,从火力地堡从Firebase中检索数据后,如何将检索到的数据从一个视图控制器发送到另一个视图控制器?

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

触发器和我的下一个视图控制器获取的所有数据之前显示空表视图。这是我的努力here

来自这篇文章here我认为我的代码放置不好。

写这行代码在你的数据传递方法,如果块之后完成循环

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

不work.Please从这篇文章仔细阅读后推到下一个视图控制器。首先,您必须完成从Firebase的所有操作。那么你有触发self.peformse .....()http://*.com/questions/37780207/good-way-to-retrieve-information-from-firebase-swift – KhanShaheb

尝试发送的搜索字符串作为发件人,当你执行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

  • 您无法发送数据的原因是因为您甚至在数据被实际提取之前尝试发送数据。
  • 请尝试以下,你只得到数据

    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") 
        } 
    
        }) 
    

    }