Как я могу проверить адрес электронной почты в UIAlertController? - PullRequest
0 голосов
/ 28 июня 2019

Я пытаюсь настроить этот UIAlertController так, чтобы кнопка «Зарегистрироваться» не становилась активной, пока не введен правильный адрес электронной почты. (Содержит символ @).

Какой-нибудь совет, как это сделать? Пробовал другие статьи, но решения не работали, Свифт новичок здесь.

Я включил код ниже:

@IBAction func loginTapped(_ sender: UIButton) {
    //The user is not logged in, so prompt for their email address        
    let loginAlert = UIAlertController(title: "Sign Up For LivNao", message: 
                     "Please enter your email address to join the LivNao study", 
                     preferredStyle: UIAlertController.Style.alert)
    loginAlert.addAction(UIAlertAction(title: "Cancel", style: 
                                       UIAlertAction.Style.cancel, handler: nil))
    loginAlert.addAction(UIAlertAction(title: "Sign Up", style: 
                         UIAlertAction.Style.default, handler: { 
                                             (action: UIAlertAction) in
                                              self.handleLogin(loginAlert)
                         }))
    loginAlert.addTextField { (textField : UITextField) in
        textField.placeholder = "Enter email"
    }
    loginAlert.view.setNeedsLayout()
    self.present(loginAlert, animated: true, completion: nil)
}

1 Ответ

0 голосов
/ 28 июня 2019

Проверьте это.

var signUp: UIAlertAction!

override func viewDidLoad() {
   super.viewDidLoad()
   // Do any additional setup after loading the view, typically from a nib.

        let loginAlert = UIAlertController(title: "Sign Up For LivNao", message: "Please enter your email address to join the LivNao study", preferredStyle: UIAlertController.Style.alert)
        let cancel = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil)
        loginAlert.addAction(cancel)
        signUp = UIAlertAction(title: "Sign Up", style: UIAlertAction.Style.default, handler: { (action: UIAlertAction) in
            //self.handleLogin(loginAlert)
        })
        loginAlert.addAction(signUp)
        loginAlert.addTextField { (textField : UITextField) in
            textField.placeholder = "Enter email"
            textField.delegate = self
        }

        signUp.isEnabled = false
        loginAlert.view.setNeedsLayout()
        self.present(loginAlert, animated: true, completion: nil)

    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
        signUp.isEnabled = isValidEmail(testStr: textField.text!) ? true : false
        return true;
    }

    func isValidEmail(testStr:String) -> Bool {
        let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"

        let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
        return emailTest.evaluate(with: testStr)
    } 
...