【问题标题】:Swift - Pass data from firebase to another ViewController (segue)Swift - 将数据从 firebase 传递到另一个 ViewController (segue)
【发布时间】:2017-01-19 11:52:22
【问题描述】:

我只想将少量数据从 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

            }

        }
    }
}

【问题讨论】:

  • 你需要在谷歌上搜索异步任务。

标签: ios swift firebase firebase-realtime-database segue


【解决方案1】:

databaseReference.child("Limit").queryOrderedByKey().observe() 异步执行代码块。这意味着当print(limits) 被调用时它还没有运行,因此limits 是空的。

【讨论】:

  • 有什么方法可以将数据保存到另一个变量并使用它们?
  • 除了最初的print()返回空,还有其他问题吗?其余的代码看起来很合理。您可以尝试在观察块中设置断点并查看它何时被调用
  • 每个单元格有3个标签
  • 是的,您可以调用 self.tableView.cellForRow(at: indexPath) (developer.apple.com/reference/uikit/uitableview/…) 来获取选定的表格单元格并在单元格上使用 viewWithTag,就像您创建它时一样
猜你喜欢
  • 2018-09-26
  • 2013-03-07
  • 2018-10-15
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
相关资源
最近更新 更多