【问题标题】:Firebase Data Returning NilFirebase 数据返回零
【发布时间】:2017-05-23 10:22:52
【问题描述】:

我正在尝试基于动态 Firebase 数据创建程序化 Radio ButtonsRadio Buttons 的数量取决于存储在 Firebase 中的整数值,名为 numberOfChildren

我从 Firebase 收到的值回来了 nil,我不知道为什么。任何有关如何解决此问题以便我可以返回整数值的帮助将不胜感激:

import UIKit
import FirebaseDatabase
import DLRadioButton


 class PollController: UIViewController {

@IBOutlet weak var passLabel: UILabel!
@IBOutlet weak var pollImage: UIImageView!

var ref: FIRDatabaseReference!
var pollRef: FIRDatabaseReference!

var pass = ""
var passedImageURL = ""

var posX = 0;
var posY = 0;

var numberOfChildren: Int!

let label2 = UILabel(frame: CGRect(x: 90, y: 160, width: 200, height: 70))

override func viewDidLoad() {
    super.viewDidLoad()
    ref = FIRDatabase.database().reference()
    pollRef = ref.child("Polls").child(pass)
    passLabel.text = pass
    pollImage.sd_setImage(with: URL(string: passedImageURL), placeholderImage: UIImage(named: "test"))

    pollRef.observe(FIRDataEventType.value, with: {(snapshot) in
        self.numberOfChildren = Int(snapshot.childSnapshot(forPath: "answers").childrenCount)
        self.passLabel.text = String(self.numberOfChildren)
        print(self.numberOfChildren)
    })

    var buttons = [DLRadioButton]()

    for x in 0..<self.numberOfChildren {
        let firstRadioButton = self.createRadioButton(frame: CGRect(x: CGFloat(x)*32, y: self.view.center.y, width: 40.0, height: 20.0), title: String(x), color: UIColor.green)
        firstRadioButton.tag = x
        buttons.append(firstRadioButton)
        self.view.addSubview(firstRadioButton);
    }

    let groupButtons = DLRadioButton()
    groupButtons.otherButtons = buttons

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

private func createRadioButton(frame : CGRect, title : String, color : UIColor) -> DLRadioButton {
    let radioButton = DLRadioButton(frame: frame);
    radioButton.titleLabel!.font = UIFont.systemFont(ofSize: 14);
    radioButton.setTitle(title, for: UIControlState.normal);
    radioButton.setTitleColor(color, for: UIControlState.normal);
    radioButton.iconColor = color;
    radioButton.indicatorColor = color;
    radioButton.contentVerticalAlignment = UIControlContentVerticalAlignment.center;
    radioButton.addTarget(self, action: #selector(self.logSelectedButton(_:)), for: UIControlEvents.touchUpInside);
    return radioButton;
}

@objc private func logSelectedButton(_ sender: DLRadioButton){

    print("Selected Button Tag = \(sender.tag) and Title \(sender.titleLabel?.text)")
   }

}

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

    问题在于您嵌套代码的方式。 Firebase 异步加载数据,这就是您传入回调块的原因:这样它就可以在数据加载后调用您的代码块。当您查看 self.numChildren 时,数据尚未加载。

    解决方法是将需要numChildren的代码移到回调块中。

    pollRef.observe(FIRDataEventType.value, with: {(snapshot) in
        self.numberOfChildren = Int(snapshot.childSnapshot(forPath: "answers").childrenCount)
        self.passLabel.text = String(self.numberOfChildren)
    
        var buttons = [DLRadioButton]()
    
        for x in 0..<self.numberOfChildren {
            let firstRadioButton = self.createRadioButton(frame: CGRect(x: CGFloat(x)*32, y: self.view.center.y, width: 40.0, height: 20.0), title: String(x), color: UIColor.green)
            firstRadioButton.tag = x
            buttons.append(firstRadioButton)
            self.view.addSubview(firstRadioButton);
        }
    
        let groupButtons = DLRadioButton()
        groupButtons.otherButtons = buttons
    })
    

    这种方式只有在 numChildren 从数据库中初始化后才会调用循环。

    【讨论】:

    • 是否需要声明为 self.numberOfChildren?
    猜你喜欢
    • 2013-10-17
    • 1970-01-01
    • 2021-03-04
    • 2021-12-13
    • 2018-09-14
    • 2020-08-20
    • 2017-09-21
    • 1970-01-01
    • 2020-12-07
    相关资源
    最近更新 更多