ios - 从 firebase 检索到 swift 3 中的多个按钮

标签 ios swift firebase swift3 firebase-realtime-database

我正在尝试从 firebase 检索数据并显示到多个按钮和标签中。 检索后我将其保存到字典中。 这是我的模型

import Foundation
import Firebase

    class QuestionModel: NSObject {

        var CorrectAnswer: String!
        var Question: String!
        var optionA: String!
        var optionB: String!
        var optionC: String!

        init(snapshot: FIRDataSnapshot) {
            if let snapshotDict = snapshot.value as? Dictionary<String, Any> {
                CorrectAnswer = snapshotDict["CorrectAnswer"] as? String
                Question = snapshotDict["Question"] as? String
                optionA = snapshotDict["optionA"] as? String
                optionB = snapshotDict["optionB"] as? String
                optionC = snapshotDict["optionC"] as? String
            }
        }
    }

我的 JSON

enter image description here

我尝试了一些方法,但它返回了我nil。我认为是异步 firebase 的原因。

这是我的代码。

import Foundation
import UIKit
import Firebase
import FirebaseDatabase

class AnsweringQuestionViewController: UIViewController {

    @IBOutlet weak var qLabel: UILabel!
    @IBOutlet weak var buttonA: UIButton!
    @IBOutlet weak var buttonB: UIButton!
    @IBOutlet weak var buttonC: UIButton!
    @IBOutlet weak var correctAnswer: UIButton!

    //var buttons: [UIButton]! thinking of using button tags?

    var ref: FIRDatabaseReference!
    var questionModel : [QuestionModel] = []

    override func viewDidLoad(){
        super.viewDidLoad()
      //  FIRDatabase.database().persistenceEnabled = true
        ref = FIRDatabase.database().reference()
        db()
    }
    func db(){
        ref.child("Science").observe(.value, with: {
            snapshot in
            for child in snapshot.children {
                let user = QuestionModel.init(snapshot: (child as? FIRDataSnapshot)!)
                self.questionModel.append(user)
            }
         self.qLabel.text = self.questionModel[0].Question
        self.buttonA.setTitle("\(self.questionModel[0].CorrectAnswer)", for: UIControlState.normal)
        }, withCancel: nil)
        }
}

请公开我的代码,我仍在学习。我不知道从这里到哪里去将我的数据显示到多个按钮中并确定哪个按钮被“单击”是正确的答案。

如果您愿意的话,如果您能以更简洁的方式重建我的代码,我将非常高兴。谢谢:)

最佳答案

试试这个...

import FirebaseDatabase

class Question {

   var correctAnswer: String?
   var question: String?
   var optionA: String?
   var optionB: String?
   var optionC: String?

   init(snapshot: FIRDataSnapshot){
       if let snap = snapshot.value as? [String: String] {
           correctAnswer = snap["CorrectAnswer"]
           question = snap["Question"]
           optionA = snap["OptionA"]
           optionB = snap["OptionB"]
           optionC = snap["OptionC"]
       }
   }

}




import FirebaseDatabase
import UIKit

class ViewController: UIViewController {

   @IBOutlet weak var qLabel: UILabel!
   @IBOutlet weak var buttonA: UIButton!
   @IBOutlet weak var buttonB: UIButton!
   @IBOutlet weak var buttonC: UIButton!
   @IBOutlet weak var correctAnswer: UIButton!

   var fireRootRef: FIRDatabaseReference!
   var questions = [Question]()

   override func viewDidLoad() {
       super.viewDidLoad()
       fireRootRef = FIRDatabase.database().reference()
       fetchQuestions(inCategory: "Science")
   }


   func fetchQuestions(inCategory category: String){
       let questionsRef = fireRootRef.child(category)

       questionsRef.observe(.value, with: {(snapshot)-> Void in

           var allQuestions = [Question]()
           for child in snapshot.children {
               allQuestions.append(Question(snapshot: child as! FIRDataSnapshot))
           }
           self.questions = allQuestions
           self.reloadUI(withQuestions: self.questions)
       })
   }

   func reloadUI(withQuestions questions: [Question]){
       guard questions.count > 0 else {return}

        if let answerToQuestion1 = questions[0].correctAnswer, let wrongAnswer = questions[0].optionA, let wrongAnswer2 = questions[0].optionB, let wrongAnswer3 = questions[0].optionC {

            let randomOrderedAnswers = [answerToQuestion1, wrongAnswer, wrongAnswer2, wrongAnswer3].shuffled()

            buttonA.setTitle(randomOrderedAnswers[0], for: .normal)
            buttonB.setTitle(randomOrderedAnswers[1], for: .normal)
            buttonC.setTitle(randomOrderedAnswers[2], for: .normal)
            correctAnswer.setTitle(randomOrderedAnswers[3], for: .normal)//keep in mind the correctAnswer label wont always contain the actual correct answer now

        } else {
            print("failed to get value for one of the answes options")
        }

       if let questionText = questions[0].question {
           qLabel.text = questionText
       } else {
           qLabel.text = "failed to get question text"
       }
   }

}




    extension MutableCollection where Indices.Iterator.Element == Index {
/// Shuffles the contents of this collection.
    mutating func shuffle() {
        let c = count
        guard c > 1 else { return }

        for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
            let d: IndexDistance = numericCast(arc4random_uniform(numericCast(unshuffledCount)))
            guard d != 0 else { continue }
            let i = index(firstUnshuffled, offsetBy: d)
            swap(&self[firstUnshuffled], &self[i])
        }
    }
}

extension Sequence {
    /// Returns an array with the contents of this sequence, shuffled.
    func shuffled() -> [Iterator.Element] {
        var result = Array(self)
        result.shuffle()
        return result
    }
}

更新 我添加了扩展,允许您打乱数组中元素的顺序,以便您可以“随机”设置答案标签,并且正确的答案不会总是在同一个位置。该扩展由 this question here 的@NateCook 提供。

要知道是否选择了正确的答案,只需检查所选按钮的文本是否 == 到问题的 CorrectAnswer 属性,例如 if buttonA.title(for: .normal)! == questions[0]. CorrectAnswer {print("选择正确答案)"} .只需确保解开 buttonA.title(for: .normal) 的可选值,否则它前面会有“可选”一词,因此不会完全等于正确的答案文本

关于ios - 从 firebase 检索到 swift 3 中的多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42841730/

相关文章:

node.js - 如何在云函数中传递动态引用?

ios - 检查 NSLocationWhenInUseUsageDescription 的接受情况

ios - 从另一个类 iOS 获取动态 bool 变量

ios - 如何在 Linux 上构建 Mac 包? (BOM文件问题)

ios - Swift:如何使用 NSUserDefaults 为 TableView 中的每个单元格存储单独的信息

ios - 使用 UITextField + UIPickerview 时禁用键盘输入

ios - 检查多个数组的特定值并计算另一个值的平均值 - swift

android - 无法从 ondatachange 方法中获取值

json - 使用 Codable Protocol 或外部工具进行 JSON 映射

iOS 动画 clipToBounds、masksToBounds 或类似的