代码之家  ›  专栏  ›  技术社区  ›  jlassap

Swift-将数据从firebase传递到另一个ViewController(segue)

  •  0
  • jlassap  · 技术社区  · 9 年前

    我只想将一些数据从Firebase传递到另一个ViewController。

    下面是我的代码。 我测试了好几次,但还是失败了。 我不知道为什么变量“limits”在线上是空的 print(limits) // IT'S EMPTY 我无法成功地将数据传递给下一个ViewController。

    import UIKit
    import Firebase
    import FirebaseDatabase
    
    struct limitStruct{
        let name : String!
        let today : String!
        let limit : String!
    
    }
    
    class CalcViewController: UITableViewController {
    
        var limits = [limitStruct]()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
            navigationController?.navigationBar.barTintColor = UIColor.black
            self.title = "Calculation"
            navigationController!.navigationBar.titleTextAttributes =
                [NSForegroundColorAttributeName: UIColor.white]
            let databaseReference = FIRDatabase.database().reference()
    
            databaseReference.child("Limit").queryOrderedByKey().observe(.childAdded, with: {
                snapshot in
                var snapshotValue = snapshot.value as? NSDictionary
    
                let name = snapshotValue!["name"] as? String
                snapshotValue = snapshot.value as? NSDictionary
    
                let today = snapshotValue!["today"] as? String
                snapshotValue = snapshot.value as? NSDictionary
    
                let limit = snapshotValue!["limit"] as? String
                snapshotValue = snapshot.value as? NSDictionary
    
                self.limits.insert(limitStruct(name:name, today:today, limit: limit), at: self.limits.count)
                self.tableView.reloadData()
            })
    
         print(limits) // IT'S EMPTY
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return limits.count
        }
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "Limit")
    
            let label1 = cell?.viewWithTag(1) as! UILabel
            label1.text = limits[indexPath.row].name
    
            let label2 = cell?.viewWithTag(2) as! UILabel
            label2.text = limits[indexPath.row].today
    
            return cell!
        }
    
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "showDetails"{
    
                let svc = segue.destination as! CirSliderViewController;
    
                if let indexPath = self.tableView.indexPathForSelectedRow{
    
                    let segueData : limitStruct
                    segueData = limits[indexPath.row]
    
                    svc.RsegueData = segueData
    
                }
    
            }
        }
    }
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   mlidal    9 年前

    databaseReference.child("Limit").queryOrderedByKey().observe() 异步执行代码块。这意味着当 print(limits) 它尚未运行,因此限制为空。

    推荐文章