我如何将self的值变为普通变量

时间:2017-05-30 23:53:24

标签: ios function variables self

如何从函数readFireBaseData()中获取self.valve1的值到函数viewDidload()?我总是得到" valve1"的设定值。这是我在计划开始时设定的。希望有人可以帮助我。

    import UIKit
    import FirebaseDatabase
    import Firebase

    class ValveViewController: UIViewController {

    var valve1    = "OFF"
    @IBOutlet var valveOne:     UILabel!

    override func viewDidLoad() {

        readFirebaseData() //run the function readFireBaseData
        let toggle = view.viewWithTag(1) as! UISwitch

        if valve1 == "ON"
        {
            toggle.setOn(true, animated: true)
        }else
        {
            toggle.setOn(false, animated: true)
        }
         super.viewDidLoad()
    }//END of viewDidLoad

    func readFirebaseData(){

        var ref: DatabaseReference!
        ref = Database.database().reference()
        ref.child("switchValve").observeSingleEvent(of: .value, with: { (snapshot) in

            let value = snapshot.value as? NSDictionary

            self.valve1 = value?["valveOne"]    as? String ?? ""
        })  
    }//End of readFireBaseData
}//End of ViewController

0 个答案:

没有答案