0

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!

enter image description here

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • Please do not post code as images. Post code as code. – dwilliss Dec 07 '17 at 22:59
  • okay thanks @dwilliss – Dont Worry Dec 07 '17 at 23:00
  • You ought not to use global variables for user-sensitive information like email addresses, if at all. At the very least, you could consider using the same viewController for both text fields with private variables or pass data betwen viewControllers with prepareForSegue(). Please can others who've done user verification in their apps contribute better strategies than what I've suggested... – Louis Leung Dec 07 '17 at 23:09

2 Answers2

0

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!
    }
}
Xcoder
  • 1,433
  • 3
  • 17
  • 37
  • When I try referencing the variable I get "Initializer for conditional binding must have Optional type, not 'String" error in the Firebase Auth part. – Dont Worry Dec 07 '17 at 23:16
  • @DontWorry don't reference the variable in an if statement, since my answer uses type `String`, not `String?`. If you want to use a variable of type `String?` and reference it in an if statement, then just change the type. It doesn't really matter. – Xcoder Dec 07 '17 at 23:21
  • referring to what @Louis Leung said I probably shouldn't use sensitive data such as an email attached to a global variable. I will just try the prepareforSegue – Dont Worry Dec 08 '17 at 00:19
  • @DontWorry Ok, sounds good. You don't have to use a global variable. – Xcoder Dec 08 '17 at 21:49
0

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.

DoesData
  • 6,594
  • 3
  • 39
  • 62