我的代码没有错误,但是控制台正在打印该帖子的标题。我正在使用解析作为服务器。我在从Udemy中学到的示例视频中使用了完全相同的代码。另外,正确连接后,我的所有按钮都无法正常工作。
import UIKit
import Parse
class ViewController: UIViewController {
var signUpMode = true
var activityIndicator = UIActivityIndicatorView()
@IBOutlet var emailTextField: UITextField!
@IBOutlet var passwordTextField: UITextField!
@IBOutlet var signUpOrLogin: UIButton!
@IBOutlet var changeSignUpMode: UIButton!
@IBOutlet var messageLabel: UILabel!
func createAlert(title:String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
//self.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
@IBAction func signUpOrLogin(_ sender: Any) {
if emailTextField.text == "" || passwordTextField.text == "" {
createAlert(title: "Error due to form", message: "Please enter an Email and Password.")
} else {
activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.style = UIActivityIndicatorView.Style.gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.shared.beginIgnoringInteractionEvents()
if signUpMode{
let user = PFUser()
user.username = emailTextField.text
user.email = emailTextField.text
user.password = passwordTextField.text
user.signUpInBackground(block:{ (success, error) in
self.activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
if error != nil {
var displayErrorMessage = "Please try again later"
if let errorMessage = (error! as NSError).userInfo["error"] as? String {
displayErrorMessage = errorMessage
//This is the message that the console gives us
}
self.createAlert(title: "Signup Error", message: displayErrorMessage)
} else {
print("user signed up")
}
})
} else {
//login mode
PFUser.logInWithUsername(inBackground: emailTextField.text!, password: passwordTextField.text!) { (user, error) in
self.activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
if error != nil{
var displayErrorMessage = "Please try again later"
if let errorMessage = (error! as NSError).userInfo["error"] as? String {
displayErrorMessage = errorMessage
//This is the message that the console gives us
}
self.createAlert(title: "Login Error", message: displayErrorMessage)
} else {
print("logged in")
}
}
}
}
}
@IBAction func changeSignUpMode(_ sender: Any) {
if signUpMode{
//Change to Login Mode
signUpOrLogin.setTitle("Log In", for: [] ) //empty array is default state again....
changeSignUpMode.setTitle("Sign Up", for: [])
messageLabel.text = "Don't have an account?"
signUpMode = false
} else {
//Change to SignupMode
signUpOrLogin.setTitle("Sign Up", for: [])
changeSignUpMode.setTitle("Log In", for: [])
messageLabel.text = "Already have an account?"
signUpMode = true
}
}