Swift IBAction迭代

时间:2017-02-26 02:29:32

标签: ios swift

我想我已编码了好几个小时,而且我正在神志不清。我需要第二只眼来看待我明显的问题。

- 单击一下,代码底部的名为(@IBAction func SignIn)的函数结束。我点击一次工作正常,点击两次代码终止并完成。如何重新迭代此功能,以便每次单击IB操作按钮时都可以继续操作。

import UIKit
import Firebase
import FirebaseDatabase
import KeychainSwift


class SignUpController: UIViewController {

    //Variable List
    var ref = FIRDatabase.database().reference() //Data Base Initialization
    public var validPassword = false        //Both passwords match

    //Represent Red or Green password Image
    @IBOutlet weak var myImageView: UIImageView!

    //INPUT - User Input from UI Text Fields
    @IBOutlet weak var emailField: UITextField!
    @IBOutlet weak var passwordField: UITextField!
    @IBOutlet weak var passwordConfirmField : UITextField!
    @IBOutlet weak var usernameField : UITextField!
    @IBOutlet weak var nameField : UITextField!

    /****************************View Loads*****************************/

    override func viewDidLoad() {
        super.viewDidLoad()

        ref = FIRDatabase.database().reference()
        myImageView.image = UIImage(named: "confirmPaswordRed")
    }

    //PROCESS - Checks is user has already been logged in
    override func viewDidAppear(_ animated: Bool) {

        let currentUser = FIRAuth.auth()?.currentUser

        if currentUser != nil {
            performSegue(withIdentifier: "SignIn", sender: nil)
        }
    }

    /******************************************************************/





    /****************************Functions*****************************/

    func CompleteSignIn(id: String){
        let keyChain = DataService().keyChain
        keyChain.set(id , forKey: "uid")
    }

    //Send all data text fields to Firebase Database
    func sendToFirebaseDatabase(userID : String) {

        let name : String = self.nameField.text!
        let username : String = self.usernameField.text!
        let userEmail : String = self.emailField.text!
        let userPassword : String = self.passwordField.text!
        self.ref.child("Users").child(userID).setValue(["Name": name, "Username": username, "Email": userEmail, "Password" : userPassword])
    }



    //Determines Red or Green password validation
    @IBAction func passwordConfirmImage(_ sender: UITextField) {

        let userConfirmPassword : String? = self.passwordConfirmField.text!
        let userPassword : String? = self.passwordField.text!

        if (userPassword == userConfirmPassword) && (userPassword != "") {

        validPassword = true
        let greenImage = UIImage(named: "confirmPasswordGreen")
        myImageView.image = greenImage
        print("Function entry test")

        }else {
            myImageView.image = UIImage(named: "confirmPaswordRed")
            validPassword = false
        }



    }

    /******************************************************************/




    /****************************IBACTIONS*****************************/







    //If SignUp button is pressed user will be directed to sign up page
    @IBAction func LoginPressed(_ sender: Any) {
        self.performSegue(withIdentifier: "BackToLogin", sender: nil)
    }

    /*Sign In Action: If both password and email contain text lets put them into string variables
                      called email & password. Creates user and authorizes sign in                                      */

    @IBAction func SignIn(_ sender: Any){




        if let email = emailField.text, let password = passwordField.text {



                  FIRAuth.auth()?.signIn(withEmail: email, password: password) { (user, error) in
                if ((error == nil) && (self.validPassword) == true) {



                    self.CompleteSignIn(id: user!.uid) //Completes Database Sign in
                    print("Sign in Test")
                    self.performSegue(withIdentifier: "SignIn", sender: nil)


                } else {
                    FIRAuth.auth()?.createUser(withEmail: email, password: password) { (user, error) in
                        if ((error == nil) && (self.validPassword) == false) {

                        Alerts().invalidSignUpAlert(sender: self) //Alert for invalid email & Password

                            print("cant sign in user") //Programmer Debugging



                        } else  {

                            if self.validPassword == true{

                                let userID : String = user!.uid //initialize userId String
                                self.sendToFirebaseDatabase(userID: userID) //Call sendToFirebaseDatabase
                                self.performSegue(withIdentifier: "SignIn", sender: nil) //goto Login Page

                            }


                        }
                    }
                }
            }

    }

  }
}

0 个答案:

没有答案