SHARE
TWEET

Untitled

a guest Jan 17th, 2020 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import UIKit
  2.  
  3.  
  4. class SignUpViewController: UIViewController {
  5.    
  6.     @IBOutlet weak var emailTextField: UITextField!
  7.     @IBOutlet weak var nameTextField: UITextField!
  8.     @IBOutlet weak var passwordTextField: UITextField!
  9.     @IBOutlet weak var passwordConfirmationTextField: UITextField!
  10.     @IBOutlet weak var nextButton: CustomButton!
  11.     @IBOutlet weak var errorLabel: UILabel!
  12.     @IBOutlet weak var birthDateTextField: UITextField!
  13.     @IBOutlet weak var genderPicker: UISegmentedControl!
  14.    
  15.     let datePicker = UIDatePicker()
  16.    
  17.     var name = ""
  18.     var email = ""
  19.     var password = ""
  20.     var birthDate = ""
  21.     var gender = ""
  22.    
  23.     override func viewDidLoad() {
  24.         super.viewDidLoad()
  25.        
  26.         birthDateTextField.inputView = datePicker
  27.         datePicker.datePickerMode = .date
  28.        
  29.         let toolbar = UIToolbar()
  30.         toolbar.sizeToFit()
  31.        
  32.         let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneAction))
  33.         let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
  34.        
  35.         toolbar.setItems([flexSpace, doneButton], animated: true)
  36.        
  37.         birthDateTextField.inputAccessoryView = toolbar
  38.     }
  39.    
  40.     //Done button of Toolbar
  41.     @objc func doneAction() {
  42.         getDateFromPicker()
  43.         view.endEditing(true)
  44.     }
  45.    
  46.     func getDateFromPicker() {
  47.         let formatter = DateFormatter()
  48.         formatter.dateFormat = "MMMM d, yyyy"
  49.         birthDateTextField.text = formatter.string(from: datePicker.date)
  50.     }
  51.    
  52.     func validateFields() -> String? {
  53.        
  54.         //Check that all fields are fillen in
  55.         if (emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
  56.             nameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
  57.             passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
  58.             passwordConfirmationTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
  59.             birthDateTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "")  {
  60.            
  61.             return "Please fill in all fields"
  62.         }
  63.         else {
  64.             if passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) != passwordConfirmationTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) {
  65.                
  66.                 return "Password and Password confirmation must be the same!"
  67.             }
  68.         }
  69.        
  70.         //TODO: Check if the password is secure
  71.        
  72.         return nil
  73.     }
  74.    
  75.     func showError(_ message:String) {
  76.         errorLabel.text = message
  77.         errorLabel.alpha = 1
  78.     }
  79.    
  80.     //MARK: Next Button Click
  81.     @IBAction func nextTapped(_ sender: Any) {
  82.        
  83.         // Create clean variables
  84.         name = nameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
  85.         email = emailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
  86.         password = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
  87.         gender = genderPicker.titleForSegment(at: genderPicker.selectedSegmentIndex) ?? "Not defined"
  88.         birthDate = birthDateTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
  89.        
  90.         // Validate the fields
  91.         let error = validateFields()
  92.        
  93.         if error != nil {
  94.             showError(error!)
  95.         }
  96.         else {
  97.            
  98.             // Transition to Agree View
  99.             performSegue(withIdentifier: "SignUpToAgreeTermsSegue", sender: self)
  100.         }
  101.     }
  102.    
  103.     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  104.         let vc = segue.destination as! AgreeTermsViewController
  105.         vc.name = self.name
  106.         vc.email = self.email
  107.         vc.password = self.password
  108.         vc.gender = self.gender
  109.         vc.birthDate = self.birthDate
  110.     }
  111.    
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top