2016-12-11 86 views
0

我在Heroku和MongoDB上部署了一個分析服務器應用程序。在我的斯威夫特3應用程序,我設置它運行在註冊功能的按鈕稱爲寄存器:反正解析註冊在Swift中無法正常工作3

@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) 
     } 
    } 

當我按下按鈕,即使在字段爲空,按鈕重定向到首頁,讓用戶使用該應用程序。我該如何解決這個問題?

enter image description here

回答

1

我的邏輯如下其中工程。另外請確保你沒有任何Segues發生,你滑入並忘記了你的storyboard

//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() 
     }} 
} 
+0

我不明白HideHUD聲明是怎麼回事。另外,成功的動作在哪裏?我有兩個視圖控制器(一個登錄和註冊一個),然後是一個主屏幕,其中檢查當前用戶 –

+0

@AlessandroThegreg,您可以刪除HideHud。這只是一個特殊的HUD,我用它來顯示用戶一個動作很忙,他需要等待。這個功能用來註冊用戶,然後自動登錄他。所以看看我的評論,你需要把你的繼續。 – Cliffordwh

+0

好吧,我明白了!謝謝 ! –