Home Firebase Auth Creating new user with text input from a different class
Reply: 2

Firebase Auth Creating new user with text input from a different class

Dont Worry
1#
Dont Worry Published in 2017-12-07 22:51:41Z

I have two view controllers, first view controller contains the textfield for the (email/username) input from the user. The second view controller contains the textfield for the (password) input from the user. The second view controller also contains a Button to execute the FireBase Auth code to create a new user. I want to take the email text from the first view controller class one and transfer that data onto to the second view controller class in order to successfully create a new user. I tried using a global variable but it doesn't seem to work, any help this dilemma? Any help is appreciated thanks!

Xcoder
2#
Xcoder Reply to 2017-12-07 23:14:43Z

A global variable, although not the best way to go about this, can work if you do it correctly:

1 Declare a global variable, which should be outside of both classes(don't use a structure):

var emailOrUsername: String = ""

2 Implement the textFieldDidEndEditing method(assuming that you have set the text field delegate to be self), and assign the string in the text field to emailOrUsername:

func textFieldDidEndEditing(_ textField: UITextField) {
    emailOrUsername = emailTextField.text
}

3 After you navigate to the next view controller, just reference that variable. Simple.

EDIT: Although this method may seem simple, when your app gradually gets more and more complicated, it will be very hard to maintain. So, I suggest you check out my answer to this question(the concept is the same: protocols).

Another method you could use is to define the emailOrUsername variable in the second viewcontroller, then, in your prepareForSegue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let secondViewController = segue.destination as? SecondViewController {
        secondViewController.emailOrUsername = emailTextField.text!
    }
}
DoesData
3#
DoesData Reply to 2017-12-07 23:08:42Z

I do not recommend the use of a global struct in this case, but if you want to do this. You can create a new swift file and move your struct out to that file. Like this:

struct GlobalData {
    var email: String? = nil
}

var globalData = GlobalData()

You can get any value in that struct using globalData.email

However, I think it makes more sense for you to pass the data when you segue. If you don't know how to make a segue look at this question. Then you can do something like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "nameOfYourSegue" {
            let controller = segue.destination as! NameOfViewControllerYourGoingTo
            controller.email = email
        }

You can look at this and this and this for more information. Let me know if you still have questions.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.359934 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO