我在Heroku和MongoDB上部署了一个解析服务器应用程序。在我的Swift 3应用程序中,我设置了一个名为register的按钮,它运行注册功能:
@IBAction func SignUpBtn(_ sender: UIButton) {
let user = PFUser()
user.username = usernameTextField.text
user.password = passwordTextField.text
user.email = emailTextField.text
user["nameandsurname"] = nameandsurnameTextField.text
user.signUpInBackground {
(succeeded: Bool, error: Error?) -> Void in
if error != nil {
print("error")
self.errorLabel.text = "Please make sure the email has not already been used or try changing the username"
} else {
self.performSegue(withIdentifier: "signuptohome", sender: self)
}
}
当我按下按钮时,即使字段为空白,该按钮也会重定向到主页,让用户使用该应用程序。我该怎么办呢?
答案 0 :(得分:1)
我的逻辑如下有效。另外,请确保您没有Segues
进入storyboard
,而是忘记了{<1}}。
//this checks 3 fields that they are filled and not empty
if passwordTextField.text == "" || emailTxt.text == "" || usernameTextField.text == ""{
simpleAlert(mess: "You must fill all fields to sign up")
self.hideHUD()
} else {
let user = PFUser()
user.username = usernameTextField.text
user.password = passwordTextField.text
user.email = emailTextField.text
user.signUpInBackground { (succeeded, error) -> Void in
if error == nil {
//success register not lets log him in!!
PFUser.logInWithUsername(inBackground: self.usernameTextField.text!, password:self.passwordTextField.text!) { (user, error) -> Void in
if error == nil {
// Here you can go to your logged in or dashboard page!!
self.hideHUD()
} else {
//failed to log user in
self.simpleAlert(mess: "\(error!.localizedDescription)")
self.hideHUD()
}}
// ERROR
} else {
//This will be the failed to register user
self.simpleAlert(mess: "\(error!.localizedDescription)")
self.hideHUD()
}}
}