如何从其他Swift文件调用数据库根目录?

时间:2017-07-21 14:22:59

标签: ios swift firebase firebase-realtime-database

我正在做一个具有第二个身份验证级别的应用程序,每当我在数据库中搜索此代码时,我会转到ViewController,显示每个代码的所有用户数据。

{
  "SchedaVolontari" : {
    "V2016040517" : {
      "CodiceVolontario" : "V2016040517",
      "Cognome" : "SANTORO",
      "DataFineServizio" : "09/01/2018",
      "DataInizioServizio" : "10/01/2017",
      "Form  Mod  1" : "OK",
      "Form  Mod  2" : "OK",
      "Form  Mod  3" : "OK",
      "Form  Mod  4" : "nil",
      "Form  Mod  5" : "nil",
      "Malattie" : 0,
      "Mon  Fin " : "nil",
      "Mon  Iniz " : "OK",
      "Mon  Int " : "nil",
      "Nome" : "SIMONA",
      "Permessi" : 15,
      "Sede" : "Universita' degli Studi di Napoli Federico II - Centro Sinapsi",
      "Titolo" : "Sostegno ed inclusione - Unina Orientale - 2015"
    },
    "V2016040518" : {
      "CodiceVolontario" : "V2016040518",
      "Cognome" : "CUSUMANO",
      "DataFineServizio" : "09/01/2018",
      "DataInizioServizio" : "10/01/2017",
      "Form  Mod  1" : "OK",
      "Form  Mod  2" : "OK",
      "Form  Mod  3" : "OK",
      "Form  Mod  4" : "OK",
      "Form  Mod  5" : "",
      "Malattie" : 0,
      "Mon  Fin " : "",
      "Mon  Iniz " : "",
      "Mon  Int " : "",
      "Nome" : "GIULIA",
      "Permessi" : 4,
      "Sede" : "Universita' degli Studi di Napoli Federico II - Centro Sinapsi",
      "Titolo" : "Sostegno ed inclusione - Unina Orientale - 2015"
    },
    "V2016040520" : {
      "CodiceVolontario" : "V2016040520",
      "Cognome" : "SERIO",
      "DataFineServizio" : "09/01/2018",
      "DataInizioServizio" : "10/01/2017",
      "Form  Mod  1" : "OK",
      "Form  Mod  2" : "OK",
      "Form  Mod  3" : "OK",
      "Form  Mod  4" : "OK",
      "Form  Mod  5" : "OK",
      "Malattie" : 1,
      "Mon  Fin " : "",
      "Mon  Iniz " : "",
      "Mon  Int " : "",
      "Nome" : "FLAVIA",
      "Permessi" : 3,
      "Sede" : "Universita' degli Studi di Napoli Federico II - Centro Sinapsi",
      "Titolo" : "Sostegno ed inclusione - Unina Orientale - 2015"
    },
    "V2016040521" : {
      "CodiceVolontario" : "V2016040521",
      "Cognome" : "LOMBARDI",
      "DataFineServizio" : "09/01/2018",
      "DataInizioServizio" : "10/01/2017",
      "Form  Mod  1" : "OK",
      "Form  Mod  2" : "OK",
      "Form  Mod  3" : "OK",
      "Form  Mod  4" : "OK",
      "Form  Mod  5" : "OK",
      "Malattie" : 0,
      "Mon  Fin " : "",
      "Mon  Iniz " : "",
      "Mon  Int " : "",
      "Nome" : "FABIO",
      "Permessi" : 2,
      "Sede" : "Universita' degli Studi di Napoli Federico II - Centro Sinapsi",
      "Titolo" : "Sostegno ed inclusione - Unina Orientale - 2015"
    },
     //..... and so on... the database is so long that i couldn't write all cases, but i given the idea.
  }
}

在第一个Swift文件中,我按子SchedaVolontari搜索我们正在谈论的用户。 一旦用户插入验证代码,这将执行另一个ViewController的segue。 现在,第二个Swift文件必须加载我们正在谈论的当前子级。我试过这种方式,但它不起作用:

这是第一个swift文件

//
//  AccessoTerzoLivello
//  Amesci
//
//  Created by Gianluca Caliendo on 2017-01-31.
//  Copyright © 2017 Gianluca Caliendo. All rights reserved.
//

import UIKit
import Firebase

class AccessoTerzoLivello: UIViewController {


    @IBOutlet weak var codvolontario: UITextField!



    @IBAction func accessoSchedaVolontari(_ sender: UIButton) {


        let rootRef = Database.database().reference()

        let conditionalRef = rootRef.child("SchedaVolontari")
    conditionalRef.observe(.value) {(snap: DataSnapshot) in

        // This is empty array which will contain all the codes
        let codesArray: NSMutableArray = []
        // Get all the children from snapshot you got back from Firebase
        let snapshotChildren = snap.children

        // Loop over all children (code) in Firebase
        while let child = snapshotChildren.nextObject() as? DataSnapshot {
            // Get code node key and save it to codes array
            codesArray.add(child.key)
        }
        //Se il codice volontario si trova nell'array principale allora...
        if codesArray.contains(self.codvolontario.text!) {


            //creazione array per distribuzione sede
            let sedeconditionalRef = rootRef.child(self.codvolontario.text!).child("Sede")
            sedeconditionalRef.observe(.value) {(snapsede: DataSnapshot) in
                // This is empty array which will contain all the codes
                let sedeArray: NSMutableArray = []
                // Get all the children from snapshot you got back from Firebase
                let snapshotChildrensede = snapsede.children
                // Loop over all children (code) in Firebase
                while let childsede = snapshotChildrensede.nextObject() as? DataSnapshot {
                    // Get code node key and save it to codes array
                    sedeArray.add(childsede.key)


                }
            }
            print(sedeConditionalRef)
            self.performSegue(withIdentifier: "segueSchedaVolontario", sender: self)
        }
        else {
            let alert = UIAlertController(title: "Errore", message: "Codice volontario non valido", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
    }

    //Torna indietro
    @IBAction func close() {
        self.dismiss(animated: true, completion: nil)
    }

}

这是第二个Swift文件

//
//  SchedaVolontario.swift
//  Amesci
//
//  Created by Gianluca Caliendo on 06/07/17.
//  Copyright © 2017 Amesci. All rights reserved.
//

import UIKit
import Firebase

class SchedaVolontario: UIViewController {


    @IBOutlet weak var SedeEnte: UILabel!



    func changesedelabel()  {

        //

        }
    }


    @IBAction func close() {
        self.dismiss(animated: true, completion: nil)
}
}

修改

我在解决方案附近。我尝试了这个解决方案并没有给我错误,但它没有返回任何结果:

这是第一个Swift文件:

    public func arraycontainer() {

        //creazione array per distribuzione sede
        let sederootRef = Database.database().reference()
        let sedeconditionalRef = sederootRef.child(self.codvolontario.text!).child("Sede")
        sedeconditionalRef.observe(.value) {(snapsede: DataSnapshot) in
            // This is empty array which will contain all the codes
            let sedeArray: NSMutableArray = []
            // Get all the children from snapshot you got back from Firebase
            let snapshotChildrensede = snapsede.children
            // Loop over all children (code) in Firebase
            while let childsede = snapshotChildrensede.nextObject() as? DataSnapshot {
                // Get code node key and save it to codes array
                sedeArray.add(childsede.value!)
            }
    }
}

这是在第二个Swift文件中:

    override func performSelector(inBackground aSelector: Selector, with arg: Any?) {
   let array = AccessoTerzoLivello.arraycontainer
    self.SedeEnte.text = array as? String
}

编辑2

First Swift文件:

func PassCode(){
    let displayViewController = storyboard!.instantiateViewController(withIdentifier: "SchedaVolontario") as! SchedaVolontario
    displayViewController.sede = self.codvolontario.text
}

第二个Swift文件:

    @IBOutlet weak var SedeEnte: UILabel!

var sede: String?

override func viewDidLoad(){
    super.viewDidLoad()
    self.SedeEnte.text = sede
print(sede!)

}

1 个答案:

答案 0 :(得分:0)

我不确定我是否仍然完全理解这个问题,但这是一个可能的答案。

假设你有像这样的Firebase结构

  "SchedaVolontari" : {
    "V2016040517" : {
      "CodiceVolontario" : "V2016040517",
      "Cognome" : "SANTORO",
      "DataFineServizio" : "09/01/2018",
      "DataInizioServizio" : "10/01/2017"
    }
    "V2016040518" : {
      "CodiceVolontario" : "V2016040518",
      "Cognome" : "CUSUMANO",
      "DataFineServizio" : "09/01/2018",
      "DataInizioServizio" : "10/01/2017"
    }

并在您的应用中显示一个视图,要求用户输入其代码。用户输入

V2016040518

with是上述结构中的第二个条目

然后,应用切换到显示用户详细信息的其他视图。假设代码已经到位以切换视图,我们通过represtedObject或一些此类技术将用户输入的代码传递给用户详细信息。

然后加载用户详细信息并将其打印到控制台:

let user_code = self.representedObject as! String

let userRef = rootRef.child("SchedaVolontari").child(user_code)
userRef.observeSingleEvent(of: .value, with: { snapshot in
     let userDict = snapshot.value as! [String: Any]
     let surname = userDict["Cognome"] as! String
     let endService = userDict["DataFineServizio"]  as! String
     let startService = userDict["DataInizioServizio"] as! String

     print("surname: \(cognome)  start: \(startService)  end: \(endService)")
})

然后打印

surname: CUSUMANO  start: 10/01/2017  end: 09/01/2018