我正在尝试根据动态Firebase数据创建程序化Radio Buttons
。 Radio 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)")
}
}
答案 0 :(得分: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
后才会调用循环。