1

I am creating an app and am using Firebase for my backend and user authentication. I complete the login process in the View Controller for the login screen. After doing so, the authData object is created in that VC, and the authData object contains a UID (user id) field. I would like to reference the uid field of the same object in another VC, in which I change the root database url by appending the myRootRef variable created in the FirebaseFramework.m file. Essentially, I am looking to be able to use and reference an object created in one VC in another VC. How can I do this?

In the login VC:

    @IBAction func LoginTapped(sender: UIButton) {
    var email:NSString = txtEmail.text as NSString
    var password:NSString = txtPassword.text as NSString

    if ( email.isEqualToString("") || password.isEqualToString("") ) {

        var alertView:UIAlertView = UIAlertView()
        alertView.title = "Sign in Failed!"
        alertView.message = "Please enter Email and Password"
        alertView.delegate = self
        alertView.addButtonWithTitle("OK")
        alertView.show()
    } else {

        myRootRef.authUser(email as String, password: password as String){
            error, authData in
            if error != nil {
                var alertView:UIAlertView = UIAlertView()
                alertView.title = "Sign in Failed!"
                alertView.message = "Incorrect email or password"
                alertView.delegate = self
                alertView.addButtonWithTitle("OK")
                alertView.show()
            } else {
              println(authData.uid)

            }
        }
    }

}

in the other VC:

@IBAction func SubmitTapped(sender: AnyObject) {   

    var UserRef = myRootRef.childByAppendingPath(authData.uid)
    UserRef.setValue(item)

}

In the other VC, "authData.uid" isn't recognized, although it is created in the login VC.

kmarwah
  • 78
  • 4
  • possible duplicate of [Accessing variables from another ViewController in Swift](http://stackoverflow.com/questions/24072766/accessing-variables-from-another-viewcontroller-in-swift) – MB41 Jul 15 '15 at 19:18

1 Answers1

3

You need to use a segue. You can wire up a segue in the storyboard, give it an identifier, and then trigger it from code.

Ray Wenderlich.com has a great article on it.

From code you can call the performSegueWithIdentifier method. This will trigger a segue. From there you can "intercept" the segue in the prepareForSegue controller method. In this method you can populate the properties of the destination view controller.

When a user is authenticated to a Firebase app the authData property contains the current user. Using that property you can populate the next View Controller.

let ref = Firebase(url: "<my-firebase-app>")

@IBAction func loginDidTouch(sender: AnyObject?) {
  ref.authUser("email", "password") { (error, authData) in
    self.performSegueWithIdentifier("MyIdentifier", sender: nil)
  }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if segue.identifier == "MyIdentifier" {
    let vc = segue.destinationViewController as! MyNextViewController
    vc.authData = ref.authData
  }
}
David East
  • 31,526
  • 6
  • 67
  • 82