我遇到了一些让segue正常工作的问题。
其他一切正常,比如保存用户名和密码进行解析。但是当用户按下sign up
或log in
时,segue
不起作用。
这是我的代码:
import UIKit
import Parse
class ViewController: UIViewController {
// Declaring variables & functions
var signupMode = true
var acitivityIndicator = UIActivityIndicatorView()
func displayAlert (title: String, message: String) {
let alertController = UIAlertController(title: title, message: title, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
// First view controller outlets/actions
@IBOutlet var signupButton: UIButton!
@IBOutlet var loginButton: UIButton!
@IBOutlet var emailTextField: UITextField!
@IBOutlet var passwordTextField: UITextField!
@IBAction func signupAction(_ sender: AnyObject) {
if emailTextField.text == "" && passwordTextField.text == "" {
displayAlert(title: "Error in form", message: "Please enter both an email and password")
} else {
acitivityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
acitivityIndicator.center = self.view.center
acitivityIndicator.hidesWhenStopped = true
acitivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
view.addSubview(acitivityIndicator)
acitivityIndicator.startAnimating()
UIApplication.shared().beginIgnoringInteractionEvents()
if signupMode {
// Sign Up
let user = PFUser()
user.username = emailTextField.text
user.password = passwordTextField.text
user.signUpInBackground(block: { (success, error) in
self.acitivityIndicator.stopAnimating()
UIApplication.shared().endIgnoringInteractionEvents()
if error != nil {
var displayErrorMessage = "Please try again later."
if let errorMessage = error?.userInfo["error"] as? String {
displayErrorMessage = errorMessage
}
self.displayAlert(title: "Signup Error", message: displayErrorMessage)
} else {
print("user signed up")
self.performSegue(withIdentifier: "HomePageViewController", sender: self)
}
})
}
}
}
@IBAction func loginAction(_ sender: AnyObject) {
acitivityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
acitivityIndicator.center = self.view.center
acitivityIndicator.hidesWhenStopped = true
acitivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
view.addSubview(acitivityIndicator)
acitivityIndicator.startAnimating()
UIApplication.shared().beginIgnoringInteractionEvents()
let userEmail = self.emailTextField.text
let userPassword = self.passwordTextField.text
PFUser.logInWithUsername(inBackground: userEmail!, password:userPassword!) {
(user: PFUser?, error: NSError?) -> Void in
if user != nil {
self.performSegue(withIdentifier: "HomePageViewController", sender: self)
} else {
self.acitivityIndicator.stopAnimating()
}
}
}
答案 0 :(得分:1)
尝试在主线程中进行segue
dispatch_async(dispatch_get_main_queue()) {
self.performSegue(withIdentifier: "HomePageViewController", sender: self)
}